How to Create a Safe git init Subcommand (with Confirmation & Smart Checks)

Published

How to Create a Safe git init Subcommand (with Confirmation & Smart Checks)
Cover image generated by AI. The words are mine, I promise.

SAFE DANGER

What?

Ever accidentally run git init in the wrong folder?

I did exactly that, by running git init on my entire coding folder instead of a particular project.

So I made a script that allows for a custom git safe-init to basically do the same thing as git-init, except it asks for confirmation and echoes back the target directory and warns you if you’re about to make a mishap.

It’s a really tiny thing but it’s a nice QOL improvement in my workflow.

Features

So, this guide will show you how to create a custom subcommand that:

  • Prevents running if you’re already inside a git repo.
  • Shows current folder clearly.
  • Stops you from making parent folders into a git repo by mistake
  • Color-coded prompts for better UX.
  • Prompts for confirmation for every git-init
  • Graceful exit if declined.

[!note] This script is written for bash and works natively on both bash and zsh — the two most common shells on macOS and Linux. You can run git safe-init from whichever shell you use daily without any changes.


Step 1: Create the Subcommand Script

We’ll place it in ~/bin/, which is a good spot for personal CLI tools. You are welcome to choose your own directory, just be sure to refer to the correct path.

mkdir -p ~/bin
nano ~/bin/git-safe-init

Step 2: Paste This Fancy Script

Amazing script, indeed.

Feel free to make changes, and if you see room for improvement - have at it!

#!/bin/bash

# --- Colors ---
RED=$'\e[31m'
GREEN=$'\e[32m'
YELLOW=$'\e[33m'
RESET=$'\e[0m'

# --- Check if we're already inside a git repo ---
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
  echo "${RED}Error:${RESET} This folder is already part of a git repository."
  echo "Use ${YELLOW}git status${RESET} to check."
  exit 1
fi

# --- Display Current Directory ---
echo "${YELLOW}You are in:${RESET} $(pwd)"

# --- Confirm Prompt ---
read -r -p "${GREEN}Are you SURE you want to 'git init' here? (y/n): ${RESET}" choice
if [[ $choice == "y" || $choice == "Y" ]]; then
  git init
else
  echo "${RED}Aborted.${RESET} No changes made."
fi

[!tip] If you’re not familiar with the nano editor in terminal, you can save + close by doing the following:

  • Press Ctrl + X.
  • Nano will ask: “Save modified buffer?” Press Y for Yes.
  • Press Enter to confirm the filename.

Step 3: Make it Executable

We need to provide permissions to that particular file where we wrote the script, so it can be executed.

chmod +x ~/bin/git-safe-init

Step 4: Add ~/bin to Your PATH (if needed)

Check if ~/bin is in your $PATH:

echo $PATH

If it is, you can proceed to Step 5 If not, add it to your shell config file and reload:

zsh (~/.zshrc):

export PATH="$HOME/bin:$PATH"
source ~/.zshrc

bash (~/.bashrc or ~/.bash_profile):

export PATH="$HOME/bin:$PATH"
source ~/.bashrc

Step 5: Usage Example

Now you can safely initialize repos like this:

git safe-init

Example Output:

You are in: /Users/you/Documents/coding-repos/my-project
Are you SURE you want to 'git init' here? (y/n): n
Aborted. No changes made.

If already inside a git repo:

Error: This folder is already part of a git repository.
Use git status to check.

Why This is Better than Raw git init

Raw git initgit safe-init
Runs instantly, no questions askedPrompts for confirmation
Easy to mess up parent foldersWarns if you’re inside another repo
No feedback if you mess upClear directory context
No color, no funUX-friendly colors

Final Thoughts

This is a simple but effective guardrail for developers who manage multiple repos and want to avoid those annoying “why is my parent folder a repo” headaches.

It feels like an official git subcommand but is fully customizable for your workflow.