Troubleshooting Git
Overview
Everyone gets Git into a tangle sometimes. The good news: Git almost never loses committed work, and almost every situation has a well-known fix. This lesson is a reference for the problems you’ll meet most often — bookmark it.
First aid
Before fixing anything:
- Read
git status. It tells you which branch you’re on, whether a merge or rebase is in progress, and usually the exact command to continue or abort. - Make a backup branch:
git branch backup. If your fix goes wrong,git reset --hard backupreturns you here. - Don’t delete the repository and re-clone in a panic — you’d lose local commits and stashes that the fixes below can save.
“I committed to the wrong branch”
You made commits on main that should be on a feature branch, and haven’t pushed:
git branch feature/profile # a new branch pointing at your commits
git reset --hard HEAD~2 # move main back by the number of commits
git switch feature/profileIf the feature branch already exists, cherry-pick the commits onto it instead, then reset main. If you already pushed them to main, don’t reset — revert them on main and cherry-pick them onto the branch.
“I’m in detached HEAD”
HEAD detached at v1.0You checked out a tag, a commit or a remote branch directly. If you just wanted to look, git switch - or git switch main. If you made commits you want to keep, create a branch before leaving: git switch -c my-fix. Already left? The commit ID is in the warning Git printed, or in git reflog.
“My push was rejected”
| Message | Cause | Fix |
|---|---|---|
! [rejected] main -> main (fetch first) |
someone pushed first | git pull --rebase, then push |
! [rejected] … (non-fast-forward) |
you rewrote history (amend/rebase) | on your own branch: git push --force-with-lease; on a shared branch: don’t — pull instead |
! [remote rejected] … (protected branch hook declined) / GH013: Repository rule violations |
branch rules | push to a branch and open a PR |
remote: Permission to … denied / 403 |
no write access or wrong account | check gh auth status, the remote URL, and your access |
Support for password authentication was removed |
typing your GitHub password | use gh auth login or SSH (Authentication with SSH and tokens) |
“You have divergent branches”
Your branch and the remote both have new commits. Choose how to combine them, and set a default so you aren’t asked again (Pushing and pulling):
git pull --rebase # or --no-rebase to merge
git config --global pull.rebase true # make it the default“I’m in the middle of a merge/rebase/cherry-pick and confused”
Every multi-step operation can be cancelled, returning to exactly where you started:
git merge --abort
git rebase --abort
git cherry-pick --abort
git revert --abortOr, to carry on after fixing conflicts: git add <files> then --continue.
“I need a file back”
git restore <file> # undo uncommitted edits
git restore --source=HEAD~3 <file> # as it was three commits ago
git log --diff-filter=D --oneline -- <file> # find the commit that deleted it…
git restore --source=<that-commit>~1 <file> # …and restore it from just before“I lost commits / a branch”
git reflog — see Recovering with reflog. Find the entry, then git branch rescue <id>.
“Every line in the file shows as changed”
Almost always line endings (CRLF vs LF) or file permissions:
git diff -wshows nothing? It’s whitespace. Add a.gitattributeswith* text=auto, and checkcore.autocrlf(Windows:true; macOS/Linux:inputor unset).old mode 100644 / new mode 100755? The executable bit changed. On file systems that don’t track it:git config core.fileMode false.
“I committed a huge file”
Git keeps every version forever, so large binaries bloat the repository, and GitHub rejects files over 100 MB.
- Not pushed yet: remove it from the commit (
git rm --cached big.zip,git commit --amend) and add it to.gitignore. - Already in history: rewrite with
git filter-repo --invert-paths --path big.zip(Keeping secrets safe). - Need large files tracked? Use Git LFS, which stores them outside the normal history.
“Git says my repository is ‘dubious ownership’”
fatal: detected dubious ownership in repository at '/srv/app'The folder belongs to a different user (common on shared servers, containers and external drives). If you trust it: git config --global --add safe.directory /srv/app.
“I accidentally ran git init in my home folder”
Delete only ~/.git (rm -rf ~/.git), which removes the accidental repository without touching your files. Double-check the path first.
When you’re stuck
git status,git log --oneline --graph --allandgit reflogtogether almost always explain the situation.git help <command>— the manual pages are thorough.- Search the exact error message; someone has had it before.
- Ask a teammate, and share the output of those three commands.
The calm approach: read the message, make a backup branch, use the escape hatch (
--abort) or the safety net (reflog). Git is hard to break permanently — if you’ve committed, you can almost always get back.
Fix five everyday messes
Five common problems, one script. (1) You made two commits on main that belong on a feature branch — move them to feature/profile and put main back. (2) You committed to the wrong message — “Fix tpyo” — fix it. (3) You started a merge that conflicts and want out. (4) git status shows you’re in detached HEAD after checking out a tag, and you made a commit — save it on a branch. (5) A file you need was deleted three commits ago — bring it back. Make a safety backup branch before you start.
mkdir mess && cd mess && git init -qb main
echo "app" > app.txt && echo "logo" > logo.svg && git add . && git commit -qm "Start app"
git tag v1.0
git rm -q logo.svg && git commit -qm "Remove old logo"
git switch -qc other && echo "other" > app.txt && git commit -qam "Other change" && git switch -q main
echo "profile v1" > profile.txt && git add . && git commit -qm "Add profile page"
echo "profile v2" > profile.txt && git commit -qam "Fix tpyo"
# 0. Safety net
# 1. Move the last two commits from main to feature/profile
# 2. Fix the "Fix tpyo" message (now on feature/profile)
# 3. A conflicting merge — get out of it
git switch -q main && echo "main change" > app.txt && git commit -qam "Main change"
# 4. Detached HEAD commit — keep it
# 5. Bring back logo.svgmkdir mess && cd mess && git init -qb main
echo "app" > app.txt && echo "logo" > logo.svg && git add . && git commit -qm "Start app"
git tag v1.0
git rm -q logo.svg && git commit -qm "Remove old logo"
git switch -qc other && echo "other" > app.txt && git commit -qam "Other change" && git switch -q main
echo "profile v1" > profile.txt && git add . && git commit -qm "Add profile page"
echo "profile v2" > profile.txt && git commit -qam "Fix tpyo"
# 0. Safety net
git branch backup
# 1. Move the last two commits from main to feature/profile
git branch feature/profile # new branch keeps the commits
git reset -q --hard HEAD~2 # main goes back (only OK because nothing was pushed)
# 2. Fix the "Fix tpyo" message (now on feature/profile)
git switch -q feature/profile
git commit -q --amend -m "Fix typo on profile page"
# 3. A conflicting merge — get out of it
git switch -q main && echo "main change" > app.txt && git commit -qam "Main change"
git merge other || true # CONFLICT in app.txt
git merge --abort
git status --short # clean again
# 4. Detached HEAD commit — keep it
git switch -q --detach v1.0
echo "hotfix" > hotfix.txt && git add . && git commit -qm "Hotfix on 1.0"
git switch -c release/1.0-hotfix # a branch now points at it
git switch -q main
# 5. Bring back logo.svg (deleted in "Remove old logo")
gone=$(git log --diff-filter=D --format=%h -1 -- logo.svg)
git restore --source="$gone~1" logo.svg
git add logo.svg && git commit -qm "Restore logo"
git branch -D backup # all fixed; remove the safety netSummary
- Before risky fixes, make a backup branch:
git branch backupcosts nothing and makes any mistake reversible. - Read
git statuscarefully — it says what state you’re in (merge, rebase, detached HEAD) and how to get out. - Wrong branch: create a branch at the current commit, then reset the old one back (if not pushed).
- Every in-progress operation has an escape:
git merge --abort,git rebase --abort,git cherry-pick --abort. - When something seems lost, it’s almost always in
git reflog.