Git Troubleshooting Cheat Sheet

Common Git errors and fixes — detached HEAD, merge conflicts, push rejected, divergent branches, authentication issues, and recovery techniques.

Last Updated: May 1, 2025

Detached HEAD

git status
Reports 'HEAD detached at commit' — you're not on a branch
git switch -c new-branch
Create a new branch at current commit (safest)
git checkout -b new-branch
Alternative: create branch at detached HEAD
git switch main
Abandon detached HEAD (commits may be lost!)
git branch temp HEAD
Save detached HEAD commits to a named branch
git reflog
Find lost detached HEAD commits by their reflog entries

Merge Conflicts

git status
Shows 'both modified' — files with conflicts
git diff --name-only --diff-filter=U
List only conflicted files
git checkout --ours file
Accept OUR version of a conflicted file
git checkout --theirs file
Accept THEIR version of a conflicted file
git mergetool
Launch visual merge tool (vscode, kdiff3, etc.)
git merge --abort
Cancel the entire merge and return to pre-merge state
git add file && git merge --continue
After resolving, stage and continue
git config --global merge.conflictstyle diff3
Show base version in conflict markers

Push Rejected

git pull --rebase origin main
Your local is behind — fetch, rebase, then push
git push --force-with-lease
Force push safely (respects others' changes)
git push -f
DANGEROUS: force push (overwrites remote completely)
git fetch origin && git diff main origin/main
See what's different on remote
git fetch origin && git rebase origin/main
Sync your branch with remote before pushing
git push -u origin branch
Set upstream: 'no upstream branch' error

Common Error Quick Fixes

ErrorCauseFix
fatal: not a git repositoryNot in a repo directorycd to repo or git init
fatal: refusing to merge unrelated historiesTwo repos with no common ancestorgit merge --allow-unrelated-histories
fatal: unable to access... SSL certificate problemSSL cert issuegit config --global http.sslVerify false (temporary)
error: Your local changes... would be overwrittenUncommitted changes conflict with checkoutgit stash then checkout
warning: LF will be replaced by CRLFLine ending mismatchgit config --global core.autocrlf input
You have divergent branchesLocal and remote divergedgit pull --rebase or git reset --hard origin/main
remote: Permission deniedSSH key or access issueCheck ssh -T [email protected]
error: failed to push some refsRemote has newer commitsPull first: git pull --rebase
Pro Tip: Stay calm — Git rarely loses data permanently. The reflog, fsck, and detached HEAD recovery can get you out of almost any mess.