https://github.com/chanware/git-safe-init
Script which acts as a safety wrapper for 'git init' that prevents accidental repository initialization in the wrong directory. Works as a proper Git subcommand.
https://github.com/chanware/git-safe-init
Last synced: 19 days ago
JSON representation
Script which acts as a safety wrapper for 'git init' that prevents accidental repository initialization in the wrong directory. Works as a proper Git subcommand.
- Host: GitHub
- URL: https://github.com/chanware/git-safe-init
- Owner: chanware
- License: mit
- Created: 2025-05-16T02:41:53.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-05-16T02:44:03.000Z (about 1 year ago)
- Last Synced: 2025-05-16T03:25:45.887Z (about 1 year ago)
- Language: Shell
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🛡️ git-safe-init
[](https://opensource.org/licenses/MIT)
[](https://www.zsh.org/)
[](https://git-scm.com/)
A safety wrapper for `git init` that prevents common mistakes.
## What Problem Does This Solve?
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.
## Features
- 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
## Installation
### Option 1: Direct Download
1. Create a personal bin directory (if you don't already have one):
```bash
mkdir -p ~/bin
```
> The `-p` flag creates parent directories as needed and doesn't error if the directory already exists - helpful for idempotent scripts.
2. Download the script:
```bash
curl -o ~/bin/git-safe-init https://raw.githubusercontent.com/chanware/git-safe-init/main/git-safe-init.sh
```
3. Make it executable:
```bash
chmod +x ~/bin/git-safe-init
```
4. Ensure ~/bin is in your PATH:
```bash
# 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
```
### Option 2: Clone the Repo
1. Clone this repository:
```bash
git clone https://github.com/chanware/git-safe-init.git
```
2. Create a symlink to your bin directory:
```bash
mkdir -p ~/bin
ln -s "$(pwd)/git-safe-init/git-safe-init.sh" ~/bin/git-safe-init
```
3. Ensure ~/bin is in your PATH (see Option 1, step 4)
## Usage
Simply run:
```
git safe-init
```
| Command | Safety | Status |
|---------|--------|--------|
| `git safe-init` | With confirmation | ✅ Safe |
| `git init` | No safety checks | ⚠️ Use with caution |
### Example Output
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.
```
## How It Works
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.
## Why Not Just an Alias?
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
## Technical Notes
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.