| name | git-troubleshooting |
| description | Git troubleshooting techniques including recovering lost commits, fixing merge conflicts, resolving detached HEAD, and diagnosing repository issues. Use when user encounters git errors or needs to recover from mistakes. |
Git Troubleshooting Skill
This skill provides comprehensive guidance on diagnosing and resolving git issues, recovering from mistakes, fixing corrupted repositories, and handling common error scenarios.
When to Use
Activate this skill when:
- Encountering git error messages
- Recovering lost commits or branches
- Fixing corrupted repositories
- Resolving detached HEAD state
- Handling botched merges or rebases
- Diagnosing repository issues
- Recovering from force push
- Fixing authentication problems
Recovering Lost Commits
Using Reflog
git reflog
git reflog show branch-name
git cherry-pick abc123
git branch recovered-branch abc123
git reset --hard abc123
Finding Dangling Commits
git fsck --lost-found
git show abc123
git branch recovered abc123
git merge abc123
Recovering Deleted Branch
git reflog
git branch feature-branch abc123
git log --all --oneline | grep "feature"
git branch feature-branch def456
Recovering After Reset
git reflog
git reset --hard def456
git branch recovery def456
Resolving Detached HEAD
Understanding Detached HEAD
git checkout abc123
git checkout v1.0.0
git checkout origin/main
Recovering from Detached HEAD
git status
git checkout -b new-branch-name
git checkout main
git checkout -b temp-branch
git checkout main
git merge temp-branch
git reflog
git branch recovery-branch abc123
Preventing Detached HEAD
git checkout -b release-v1.0 v1.0.0
git checkout -b local-feature origin/feature-branch
git symbolic-ref -q HEAD && echo "attached" || echo "detached"
Fixing Merge Conflicts
Understanding Conflict Markers
<<<<<<< HEAD (Current Change)
int result = add(a, b);
||||||| merged common ancestors (Base)
int result = sum(a, b);
=======
int sum = calculate(a, b);
>>>>>>> feature-branch (Incoming Change)
Basic Conflict Resolution
git status
cat file.go
git checkout --ours file.go
git add file.go
git checkout --theirs file.go
git add file.go
git add file.go
git commit
Aborting Operations
git merge --abort
git rebase --abort
git cherry-pick --abort
git revert --abort
git am --abort
Complex Conflict Resolution
git mergetool
git diff --ours
git diff --theirs
git diff --base
git diff --check
git diff --name-only --diff-filter=U
git add .
git commit
Fixing Botched Rebase
Recovering from Failed Rebase
git rebase --abort
git reflog
git reset --hard HEAD@{1}
git reset --hard ORIG_HEAD
Rebase Conflicts
git status
git add file.go
git rebase --continue
git rebase --skip
git commit --amend
git rebase --continue
Rebase onto Wrong Branch
git reflog
git reset --hard HEAD@{5}
git rebase correct-branch
Repository Corruption
Detecting Corruption
git fsck --full
git fsck --connectivity-only
git verify-pack -v .git/objects/pack/*.idx
Fixing Corrupted Objects
rm .git/objects/ab/cd1234...
git fetch origin
git gc --prune=now
git gc --aggressive --prune=now
Fixing Index Corruption
rm .git/index
git reset
git reset --hard HEAD
Recovering from Bad Pack File
git unpack-objects < .git/objects/pack/pack-*.pack
rm .git/objects/pack/pack-*
git repack -a -d
git fsck --full
Fixing References
Corrupted Branch References
git show-ref
echo "abc123def456" > .git/refs/heads/branch-name
git update-ref refs/heads/branch-name abc123
git update-ref -d refs/heads/bad-branch
Fixing HEAD Reference
echo "ref: refs/heads/main" > .git/HEAD
echo "abc123def456" > .git/HEAD
git symbolic-ref HEAD
Pruning Stale References
git remote prune origin
git fetch --prune
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
Undoing Changes
Undo Last Commit (Keep Changes)
git reset --soft HEAD~1
git reset HEAD~1
Undo Last Commit (Discard Changes)
git reset --hard HEAD~1
git reflog
git reset --hard HEAD@{1}
Undo Multiple Commits
git reset --hard abc123
git revert HEAD~3..HEAD
git rebase -i HEAD~5
Undo Changes to File
git checkout -- file.go
git restore file.go
git reset HEAD file.go
git restore file.go
git restore --staged file.go
git restore file.go
git checkout abc123 -- file.go
Undo Public Commits
git revert HEAD
git revert abc123
git revert abc123..def456
git revert -m 1 merge-commit-hash
Handling Force Push Issues
Recovering After Force Push
git reflog
git reset --hard HEAD@{1}
git branch backup-branch
git pull origin main
git merge backup-branch
Preventing Force Push Damage
git fetch origin
git log origin/main..HEAD
git push --force-with-lease origin main
git config --global push.default simple
Large File Issues
Finding Large Files
git rev-list --objects --all | \
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
awk '/^blob/ {print substr($0,6)}' | \
sort --numeric-sort --key=2 | \
tail -10
git ls-files | xargs ls -lh | sort -k 5 -h -r | head -20
Removing Large Files
git filter-repo --path large-file.bin --invert-paths
bfg --strip-blobs-bigger-than 100M
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --force origin main
Preventing Large Files
cat > .git/hooks/pre-commit << 'EOF'
if git diff --cached --name-only | xargs du -h | awk '$1 ~ /M$|G$/' | grep .; then
echo "Error: Large file detected"
exit 1
fi
EOF
chmod +x .git/hooks/pre-commit
Authentication Issues
SSH Key Problems
ssh -T git@github.com
ls -la ~/.ssh
ssh-keygen -t ed25519 -C "email@example.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
cat > ~/.ssh/config << EOF
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
EOF
HTTPS Authentication
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
git config --global credential.helper store
git remote set-url origin https://github.com/user/repo.git
git clone https://TOKEN@github.com/user/repo.git
Permission Denied
git remote -v
git remote set-url origin git@github.com:user/repo.git
git remote set-url origin https://github.com/user/repo.git
ls -la .git/
chmod -R u+rw .git/
Submodule Issues
Submodule Not Initialized
git submodule init
git submodule update
git submodule update --init --recursive
Detached HEAD in Submodule
cd submodule-dir
git checkout -b main
git checkout main
git pull origin main
cd ..
git add submodule-dir
git commit -m "chore: update submodule"
Submodule Conflicts
git submodule status
git submodule update --init --force
git submodule deinit -f path/to/submodule
git rm -f path/to/submodule
git submodule add <url> path/to/submodule
Performance Issues
Slow Operations
git gc --aggressive
git repack -a -d --depth=250 --window=250
git prune --expire now
git clean -fdx
Large Repository
git clone --depth 1 <url>
git clone --single-branch --branch main <url>
git clone --filter=blob:none <url>
git sparse-checkout init --cone
git sparse-checkout set folder1 folder2
Memory Issues
git config --global pack.windowMemory "100m"
git config --global pack.packSizeLimit "100m"
git config --global pack.threads "1"
git config --global pack.compression 0
Common Error Messages
"fatal: refusing to merge unrelated histories"
git pull origin main --allow-unrelated-histories
"fatal: not a git repository"
git init
ls -la .git
"error: Your local changes would be overwritten"
git stash
git pull
git stash pop
git reset --hard HEAD
git pull
"error: failed to push some refs"
git pull origin main
git pull --rebase origin main
git push --force origin main
git push --force-with-lease origin main
"fatal: unable to access: SSL certificate problem"
git config --global http.sslVerify false
git config --global http.sslCAInfo /path/to/cacert.pem
Diagnostic Commands
Repository Health Check
git fsck --full --strict
git fsck --connectivity-only
git verify-pack -v .git/objects/pack/*.idx
git reflog expire --dry-run --all
git count-objects -vH
Debug Information
GIT_TRACE=1 git status
GIT_TRACE=1 git pull
GIT_TRACE_PACKET=1 git fetch
GIT_TRACE_PERFORMANCE=1 git diff
GIT_CURL_VERBOSE=1 git push
git config --list --show-origin
git config --list --show-scope
Prevention Strategies
- Regular Backups: Create backup branches before risky operations
- Use Reflog: Reflog is your safety net, keep it clean
- Enable Rerere: Reuse recorded conflict resolutions
- Protect Branches: Use branch protection rules
- Pre-commit Hooks: Validate commits before they're made
- Regular Maintenance: Run
git gc periodically
- Test Before Force Push: Always verify with
--dry-run
- Communication: Inform team about disruptive operations
- Learn Git Internals: Understanding how git works prevents issues
- Keep Git Updated: Use latest stable version
Resources
Additional troubleshooting guides are available in the assets/ directory:
troubleshooting/ - Step-by-step recovery procedures
scripts/ - Diagnostic and recovery scripts
checklists/ - Problem diagnosis workflows
See references/ directory for:
- Git error message database
- Recovery procedure documentation
- Git internal structure guides
- Common pitfall documentation