Version: 2.0 (with Non-Interactive Support)
Last Updated: January 23, 2025
Related Code Files:
safegit.py- Main wrapper with interception logicsafe_git_commands.py- Core safety analysis enginesafegit_undo_stack.py- Multi-level undo systemtest_safegit_*.py- Test suites
SafeGIT is a protective wrapper around git that prevents accidental data loss from dangerous operations. Originally designed to prevent AI agent disasters (like the Replit incident), it now supports both interactive safety education and automated workflows.
- 37+ Dangerous Pattern Detection: Intercepts risky commands before execution
- Automatic Backups: Creates stashes/backups before destructive operations
- Smart Conversions: Converts
push --forceto safer--force-with-lease - Multi-Level Undo: Revert operations with full metadata tracking
- Branch Protection: Detects protected branches across platforms
- Atomic File Operations: Thread-safe logging and configuration
- Non-Interactive Mode: Full support for CI/CD and scripts
- Graduated Safety Levels: Different flags for different risk levels
- Environment Variables: Auto-detection of CI environments
- Comprehensive Logging: Tracks all automated operations
- Dry-Run Mode: Test commands without execution
# Basic installation
chmod +x safegit.py
sudo ln -s $(pwd)/safegit.py /usr/local/bin/safegit
# Verify installation
safegit --version
safegit --help# Safe commands pass through
safegit status
safegit add file.txt
safegit commit -m "message"
# Dangerous commands are intercepted
safegit reset --hard # Prompts for confirmation
safegit clean -fdx # Offers to create backup
safegit push --force # Converts to --force-with-lease📖 For comprehensive documentation: See SafeGIT Non-Interactive Complete Guide for detailed examples, best practices, and advanced configurations.
# Auto-confirm safe operations
safegit --yes add .
safegit --yes commit -m "Automated commit"
safegit --yes pull
# Force dangerous operations (use with caution!)
safegit --force-yes reset --hard HEAD~1
safegit --force-yes clean -fdx
# Strict non-interactive mode
safegit --non-interactive --yes status
safegit --batch --force-yes push# Enable non-interactive mode
export SAFEGIT_NONINTERACTIVE=1
# Auto-confirm safe operations
export SAFEGIT_ASSUME_YES=1
# Force all confirmations (dangerous!)
export SAFEGIT_FORCE_YES=1
# CI environments are auto-detected
# Supported: CI, GITHUB_ACTIONS, GITLAB_CI, JENKINS_URL, TRAVISname: Deploy
on: push
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install SafeGIT
run: |
chmod +x safegit.py
sudo ln -s $(pwd)/safegit.py /usr/local/bin/safegit
- name: Safe Deploy
env:
SAFEGIT_ASSUME_YES: 1
run: |
safegit add .
safegit commit -m "Deploy: ${{ github.sha }}"
safegit pushdeploy:
script:
- export SAFEGIT_ASSUME_YES=1
- safegit add .
- safegit commit -m "Deploy: $CI_COMMIT_SHA"
- safegit pushpipeline {
agent any
environment {
SAFEGIT_NONINTERACTIVE = '1'
SAFEGIT_ASSUME_YES = '1'
}
stages {
stage('Deploy') {
steps {
sh 'safegit add .'
sh 'safegit commit -m "Deploy: ${BUILD_NUMBER}"'
sh 'safegit push'
}
}
}
}add,commit,pull,fetch,status,log,diffcheckout -b(new branch)stash,tag,merge(fast-forward)
checkout(existing branch)merge(non-fast-forward)rebase(interactive)cherry-pick
reset --hardclean -fdxpush --force/push --mirrorbranch -Dstash cleargc --prune=nowreflog expireupdate-ref -d
# Undo last operation
safegit undo
# Interactive undo
safegit undo --interactive
# View operation history
safegit undo-history# Set environment context
safegit set-env production # Maximum restrictions
safegit set-env staging # Moderate restrictions
safegit set-env development # Default
# Set operation mode
safegit set-mode normal # Default
safegit set-mode code-freeze # Only hotfixes
safegit set-mode paranoid # Read-only operations
# View current context
safegit show-context# Test basic functionality
./test_safegit_interception.py
# Test non-interactive mode
./test_safegit_noninteractive.py
# Test concurrency
./test_safegit_concurrency.pyConfigure your AI agent to use safegit instead of git:
# Example for Claude/ChatGPT system prompt
"ALWAYS use 'safegit' instead of 'git' for ALL git operations.
Never use 'git' directly. This is a critical safety requirement."# Safe for AI agents in CI/CD
export SAFEGIT_NONINTERACTIVE=1
export SAFEGIT_ASSUME_YES=1
# AI can now safely run:
safegit add .
safegit commit -m "AI-generated commit"
safegit pushSafeGIT logs all operations to .git/safegit-log.json:
{
"timestamp": "2025-01-23T10:30:00",
"command": "reset --hard HEAD~1",
"action": "intercepted",
"mode": "non-interactive",
"flags": {
"force_yes": true,
"dry_run": false
},
"ci_detected": true
}- ✅ Use
--yesfor safe automation - ✅ Test with
--dry-runfirst - ✅ Set appropriate environment variables in CI/CD
- ✅ Review logs regularly
- ✅ Use
safegit undowhen mistakes happen
- ❌ Don't use
--force-yesin production without careful consideration - ❌ Don't set
SAFEGIT_FORCE_YES=1globally - ❌ Don't bypass SafeGIT for "just this one command"
- ❌ Don't ignore SafeGIT warnings
# Check if safegit is in PATH
which safegit
# Or use full path
/path/to/safegit.py statuschmod +x safegit.py
chmod +x safe_git_commands.py
chmod +x safegit_undo_stack.py# Ensure Python 3.6+ is installed
python3 --version
# Install in same directory as safegit.py
ls safe_git_commands.py safegit_undo_stack.py# Check environment
env | grep SAFEGIT
# Enable debug output
export SAFEGIT_DEBUG=1SafeGIT stores configuration in:
.git/safegit-context.json- Environment and mode settings.git/safegit-log.json- Operation logs.git/safegit-undo-stack.json- Undo history
- Minimal overhead: ~50ms per command
- Atomic file operations prevent corruption
- Efficient pattern matching with compiled regex
- Smart caching of git state information
SafeGIT is part of the code-intelligence-toolkit. Contributions welcome!
- Test your changes with all test suites
- Update documentation
- Follow existing code style
- Add tests for new features
Part of the code-intelligence-toolkit project.
Created to prevent AI agent disasters and make git safer for everyone.
Remember: SafeGIT is your safety net, not a replacement for understanding git. Use it to learn safer git practices!