Troubleshooting Git

Topic 2 of 3 in Production8 min readBeginnerFree course

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:

  1. 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.
  2. Make a backup branch: git branch backup. If your fix goes wrong, git reset --hard backup returns you here.
  3. 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 want to undo… which command? Edits in a file I have not staged: git restore <file>. A file I staged by mistake: git restore –staged <file>. My last commit message: git commit –amend. My last commit, keeping the changes: git reset –soft HEAD1. A commit that is already pushed: git revert <commit>. A merge in progress: git merge –abort. A rebase in progress: git rebase –abort. Something I “lost”: git reflog, then git branch rescue <id>. Commits made on the wrong branch: git branch new; git reset –hard HEADn. Red means it can discard work. When unsure, make a backup branch first: git branch backup.

“I committed to the wrong branch”

You made commits on main that should be on a feature branch, and haven’t pushed:

Terminal
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/profile

If 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”

Text
HEAD detached at v1.0

You 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):

Terminal
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:

Terminal
git merge --abort
git rebase --abort
git cherry-pick --abort
git revert --abort

Or, to carry on after fixing conflicts: git add <files> then --continue.

“I need a file back”

Terminal
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 -w shows nothing? It’s whitespace. Add a .gitattributes with * text=auto, and check core.autocrlf (Windows: true; macOS/Linux: input or 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’”

Text
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 --all and git reflog together 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.

Exercise

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.

Terminal
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.svg

Summary

  • Before risky fixes, make a backup branch: git branch backup costs nothing and makes any mistake reversible.
  • Read git status carefully — 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.