This document provides comprehensive safety guidelines for Git operations when using CLAUDE Code, based on analysis of all 194+ Git commands and their potential risks.
- Total Git commands analyzed: 194 commands and aliases
- Safe operations: 45+ read-only and basic development commands
- Development operations: 35+ commands requiring confirmation
- Restricted operations: 40+ potentially dangerous commands
- Forbidden operations: 25+ extremely dangerous commands
Commands safe for read-only operations and information gathering:
git status,git log,git show,git diff,git blamegit ls-files,git ls-tree,git ls-remote,git describegit branch --list,git tag --list,git remote -vgit config --list,git reflog show,git help
git fsck,git count-objects,git verify-packgit check-ref-format,git check-attr,git check-ignoregit grep,git cherry,git merge-base
git add --dry-run,git push --dry-run,git clean --dry-rungit merge --dry-run,git rebase --dry-run,git gc --dry-run
Normal development operations requiring user confirmation:
git add,git commit,git commit -m,git commit --amendgit checkout,git switch,git restore,git mvgit branch,git tag,git merge,git pull,git fetch
git clone,git init,git remote add,git remote set-urlgit stash,git cherry-pick,git revert,git rebasegit reset --soft,git reset --mixed
git config --add,git config --set,git config --unsetgit submodule add,git submodule update,git worktree add
Operations that can cause data loss or significant changes:
git clean,git clean -f,git clean -fdgit rm,git rm -f,git rm -rgit reset --hard,git reset --merge
git branch -d,git branch -D,git branch -fgit tag -d,git tag -fgit remote remove,git remote prune
git reflog delete,git reflog expiregit filter-branch,git replacegit push --force,git push --force-with-lease
Extremely dangerous operations that should never run automatically:
git clean -ffx,git rm -rf *,git rm -rf .git reset --hard HEAD~,git reset --hard origin/mastergit push --force origin master,git push --delete origin master
git config --global user.email,git config --system core.hooksPathgit daemon,git daemon --export-allgit fast-import,git cvsserver,git svn
git branch -D master,git tag -d v*git reflog expire --expire=now --allgit submodule foreach --recursive git clean -ffxd
# DANGEROUS: Hard resets to arbitrary commits
git reset --hard HEAD~5 # Loses 5 commits permanently
git reset --hard origin/master # May lose local work
git reset --hard <commit-hash> # Destructive without backup
# SAFER ALTERNATIVES:
git stash # Save current work
git reset --soft HEAD~5 # Keep changes in staging
git reset --mixed HEAD~5 # Keep changes in working directory# EXTREMELY DANGEROUS:
git push --force origin master # Can overwrite others' work
git push -f origin main # Same danger as above
git push --delete origin important-branch # Deletes remote branch
# SAFER ALTERNATIVES:
git push --force-with-lease origin feature-branch # Safer force push
git push --dry-run origin feature-branch # Test first
git pull --rebase origin master # Integrate changes first# DANGEROUS:
git clean -ffxd # Removes ALL untracked files and directories
git rm -rf * # Removes all tracked files
git reflog expire --all # Deletes all reflog history
git prune --expire=now # Removes all unreachable objects immediately
# SAFER ALTERNATIVES:
git clean -n # Preview what would be deleted
git clean -i # Interactive deletion
git stash -u # Stash untracked files instead# DANGEROUS:
git config --global user.email malicious@example.com # Identity theft
git config --system core.hooksPath /malicious/path # Hook injection
git config receive.denyNonFastForwards false # Disables safety
# SAFER ALTERNATIVES:
git config user.email your-email@example.com # Local config only
git config --list # Review current config
git config --unset dangerous.setting # Remove risky settingsNever allow destructive operations on these branches:
main,master,develop,staging,productionrelease/*,hotfix/*,v*.*.*
These branches are generally safer for experiments:
feature/*,bugfix/*,experimental/*,test/*personal/*,draft/*,wip/*
# SAFE: Working on feature branches
git checkout -b feature/new-implementation
git commit -m "Add new feature"
git push origin feature/new-implementation
# DANGEROUS: Directly modifying main branches
git checkout main
git reset --hard HEAD~5 # NEVER DO THIS ON MAIN
git push --force origin main # EXTREMELY DANGEROUS- Personal development projects
- Feature branches and experiments
- Local testing repositories
- Forked repositories
- Shared team repositories
- Main/master branches
- Release repositories
- CI/CD repositories
- Production repositories
- System configuration repositories
- Bare repositories with hooks
- External/third-party repositories
--dry-run,-n: Preview operations--verbose,-v: Detailed output--help: Documentation--list: List items--show-current: Display current state
--force,-f: Override safety checks--hard: Destructive reset--aggressive: Intensive operations--all: Bulk operations--delete,-D: Deletion operations
--system: System-wide configuration--global: Global configuration changes--exec: Execute arbitrary commands--shared: Shared repository setup
# Problem: git reset --hard HEAD~5
# Recovery:
git reflog show HEAD
git reset --hard HEAD@{1} # Go back to before the reset# Problem: git branch -D important-feature
# Recovery:
git reflog show --all | grep important-feature
git checkout -b important-feature <commit-hash># Problem: git push --force origin master
# Recovery (if others haven't pulled yet):
git reflog show origin/master
git push --force-with-lease origin <previous-commit>:master# Problem: git clean -fd
# Recovery: Git clean operations are NOT recoverable
# Prevention: Always use git clean -n first# Problem: Committed with wrong identity
# Recovery:
git commit --amend --reset-author
git rebase -i HEAD~N --exec "git commit --amend --reset-author --no-edit"- Repository State Check: Verify clean working directory
- Branch Protection: Prevent operations on protected branches
- Remote Safety: Validate remote repository URLs
- Backup Creation: Auto-create reflog backups for dangerous operations
- Operation Logging: Record all Git commands executed
- Change Detection: Monitor for unexpected repository changes
- Safety Metrics: Track dangerous operation frequency
- Recovery Assistance: Provide recovery suggestions for failures
# Set up safer Git defaults
git config --global push.default simple
git config --global pull.rebase true
git config --global rebase.autoStash true
git config --global core.autocrlf input
git config --global init.defaultBranch main
# Enable safety features
git config --global advice.pushNonFastForward true
git config --global advice.statusHints true
git config --global advice.commitBeforeMerge trueFor any potentially destructive operation:
git clean -n # Instead of git clean -f
git push --dry-run # Before git push --force
git merge --dry-run # Before git mergeBefore dangerous operations:
git stash push -m "Safety checkpoint before operation"
git tag safety-checkpoint-$(date +%Y%m%d-%H%M%S)# Instead of git reset --hard
git stash && git reset --soft
# Instead of git push --force
git push --force-with-lease
# Instead of git clean -f
git clean -i # Interactive modegit status --porcelain
git log --oneline -n 10
git reflog --oneline -n 10
git fsck --no-dangling- Stop immediately: Use Ctrl+C if still running
- Check damage:
git status,git log --oneline - Use reflog:
git reflog show HEAD - Restore if possible:
git reset --hard HEAD@{N} - Contact team: If shared repository affected
- Assess the situation: What command was run?
- Check reflog:
git reflog show --all - Look for backups: Check for stashes, tags, other branches
- Restore from remote:
git fetch origin && git reset --hard origin/branch - Last resort: Restore from file system backup
- Load
claude-git-permissions.jsonconfiguration - Implement command categorization system
- Add pre-execution validation hooks
- Create confirmation prompts for restricted operations
- Block forbidden operations completely
- Log all git operations with timestamps
- Provide recovery suggestions for failed operations
- Monitor repository health after operations
- Validate repository ownership before operations
- Check for hooks that might execute malicious code
- Verify remote URLs are safe
- Prevent operations on system directories
- Block execution of Git aliases that might be dangerous
- Monitor for suspicious configuration changes
This comprehensive safety system provides multiple layers of protection while maintaining development productivity. The key is progressive permission levels: safe operations run automatically, development operations require confirmation, restricted operations need explicit approval, and forbidden operations are completely blocked.%