A safety wrapper for git init
that prevents common mistakes.
If you've ever accidentally initialized a git repository in the wrong directory (like your parent "repos" folder), you know the headache that follows. This script adds a confirmation layer to prevent those mistakes.
- Detects if you're already inside an existing git repo
- Shows your current working directory for verification
- Requires explicit confirmation before proceeding
- Works as a proper git subcommand (
git safe-init
) - Includes visual formatting to highlight important information
-
Create a personal bin directory (if you don't already have one):
mkdir -p ~/bin
The
-p
flag creates parent directories as needed and doesn't error if the directory already exists - helpful for idempotent scripts. -
Download the script:
curl -o ~/bin/git-safe-init https://raw.githubusercontent.com/chanware/git-safe-init/main/git-safe-init.sh
-
Make it executable:
chmod +x ~/bin/git-safe-init
-
Ensure ~/bin is in your PATH:
# Check if it's already there echo $PATH | grep -q "$HOME/bin" && echo "Already in PATH" || echo "Not in PATH" # Add to PATH if needed (for zsh) echo 'export PATH="$HOME/bin:$PATH"' >> ~/.zshrc source ~/.zshrc
-
Clone this repository:
git clone https://github.com/chanware/git-safe-init.git
-
Create a symlink to your bin directory:
mkdir -p ~/bin ln -s "$(pwd)/git-safe-init/git-safe-init.sh" ~/bin/git-safe-init
-
Ensure ~/bin is in your PATH (see Option 1, step 4)
Simply run:
git safe-init
Command | Safety | Status |
---|---|---|
git safe-init |
With confirmation | ✅ Safe |
git init |
No safety checks |
When run in a new directory:
You are in: /Users/you/projects/new-project
Are you SURE you want to 'git init' here? (y/n): y
Initialized empty Git repository in /Users/you/projects/new-project/.git/
When already inside a git repository:
Error: This folder is already part of a git repository.
Use git status to check.
This uses Git's subcommand mechanism, where executables named git-*
in your PATH become available as git *
commands. The script performs safety checks before delegating to the actual git init
command.
While you could create a shell alias, this approach:
- Works consistently across different shell environments
- Follows Git's established extension pattern
- Is easier to share with others
- Doesn't require modifying shell configs on each machine
The script uses zsh by default. If you prefer bash, change the shebang line from #!/bin/zsh
to #!/bin/bash
and modify the color formatting.