Last Updated: May 1, 2025
Detached HEAD
git statusReports 'HEAD detached at commit' — you're not on a branch
git switch -c new-branchCreate a new branch at current commit (safest)
git checkout -b new-branchAlternative: create branch at detached HEAD
git switch mainAbandon detached HEAD (commits may be lost!)
git branch temp HEADSave detached HEAD commits to a named branch
git reflogFind lost detached HEAD commits by their reflog entries
Merge Conflicts
git statusShows 'both modified' — files with conflicts
git diff --name-only --diff-filter=UList only conflicted files
git checkout --ours fileAccept OUR version of a conflicted file
git checkout --theirs fileAccept THEIR version of a conflicted file
git mergetoolLaunch visual merge tool (vscode, kdiff3, etc.)
git merge --abortCancel the entire merge and return to pre-merge state
git add file && git merge --continueAfter resolving, stage and continue
git config --global merge.conflictstyle diff3Show base version in conflict markers
Push Rejected
git pull --rebase origin mainYour local is behind — fetch, rebase, then push
git push --force-with-leaseForce push safely (respects others' changes)
git push -fDANGEROUS: force push (overwrites remote completely)
git fetch origin && git diff main origin/mainSee what's different on remote
git fetch origin && git rebase origin/mainSync your branch with remote before pushing
git push -u origin branchSet upstream: 'no upstream branch' error
Common Error Quick Fixes
| Error | Cause | Fix |
|---|---|---|
| fatal: not a git repository | Not in a repo directory | cd to repo or git init |
| fatal: refusing to merge unrelated histories | Two repos with no common ancestor | git merge --allow-unrelated-histories |
| fatal: unable to access... SSL certificate problem | SSL cert issue | git config --global http.sslVerify false (temporary) |
| error: Your local changes... would be overwritten | Uncommitted changes conflict with checkout | git stash then checkout |
| warning: LF will be replaced by CRLF | Line ending mismatch | git config --global core.autocrlf input |
| You have divergent branches | Local and remote diverged | git pull --rebase or git reset --hard origin/main |
| remote: Permission denied | SSH key or access issue | Check ssh -T [email protected] |
| error: failed to push some refs | Remote has newer commits | Pull 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.