11. Troubleshooting & Debugging Git Issues
Troubleshooting & Debugging Git Issues
Section titled “Troubleshooting & Debugging Git Issues”Sooner or later Git will show you a scary-looking message, and your first instinct might be to panic. Don’t. Almost every Git “disaster” is recoverable, and most error messages are actually telling you precisely how to fix them. This chapter walks through the situations you’ll hit most often.
Fixing detached HEAD state
Section titled “Fixing detached HEAD state”We met HEAD in Chapter 7: normally it points to a branch. Sometimes, though, you check out a specific commit or tag directly:
git checkout a1b2c3dGit warns you that you’re now in a detached HEAD state:
You are in 'detached HEAD' state. You can look around, make experimentalchanges and commit them...This isn’t an error. It just means HEAD is pointing straight at a commit instead of a branch. You can look around freely. The danger is only if you make new commits here: because no branch points to them, they can be lost once you switch away.
If you just wanted to inspect things, simply go back to your branch:
git switch mainIf you made commits you want to keep, create a branch from where you are before switching away:
git switch -c my-rescued-workNow those commits live on a real branch and are safe. (And if you already switched away and panicked, remember the reflog from Chapter 8 can bring them back.)
Dealing with merge conflicts
Section titled “Dealing with merge conflicts”We covered the mechanics of resolving conflicts in Chapter 4, so here’s a quick troubleshooting checklist for when you’re stuck mid-conflict and unsure what to do:
git status # Which files are still in conflict?git diff # What exactly is disputed?- To accept your branch’s version of a file wholesale:
git checkout --ours <file> - To accept the incoming branch’s version:
git checkout --theirs <file> - One gotcha: during a rebase,
--oursand--theirsare swapped compared to a merge, because a rebase replays your commits on top of the other branch. When in doubt, checkgit status, which names the branches explicitly. - After editing a file by hand, mark it resolved with
git add <file>. - To bail out completely and pretend the merge never started:
git merge --abort.
Resolving git push and git pull errors
Section titled “Resolving git push and git pull errors”These two commands produce the errors people search for most. The good news: the messages are usually self-explanatory once you know what they mean.
”Updates were rejected because the remote contains work you do not have”
Section titled “”Updates were rejected because the remote contains work you do not have””! [rejected] main -> main (fetch first)error: failed to push some refs to '...'This means someone pushed to the remote since you last synced. Git is protecting you from overwriting their work. The fix is to bring their changes in first, then push:
git pull --rebasegit push”The current branch has no upstream branch”
Section titled “”The current branch has no upstream branch””fatal: The current branch feature has no upstream branch.Git doesn’t know where to push because the branch isn’t linked to a remote one yet. Set it up while pushing:
git push -u origin feature“Your local changes would be overwritten by merge”
Section titled ““Your local changes would be overwritten by merge””error: Your local changes to the following files would be overwritten by mergeA git pull can’t proceed because you have uncommitted edits that conflict. You have two clean options: commit your work, or stash it (from Chapter 8) and reapply it afterward:
git stashgit pullgit stash pop“Permission denied (publickey)” or “Authentication failed”
Section titled ““Permission denied (publickey)” or “Authentication failed””These mean the platform rejected your credentials, not that anything is wrong with your repository.
Over SSH (a git@github.com:... remote), Permission denied (publickey) means your SSH key isn’t registered with the platform (or isn’t loaded into your agent). Generate a key, add the public half to your account, then test the connection:
ssh -T git@github.comOver HTTPS (a https://... remote), Authentication failed almost always means you entered your account password. As noted in Chapter 13, platforms no longer accept passwords for Git operations: create a personal access token and use it in place of the password.
Diagnosing issues with git fsck and git gc
Section titled “Diagnosing issues with git fsck and git gc”When you suspect something is genuinely wrong with the repository itself (not just your workflow), Git has two low-level tools to inspect and tidy its internal database.
git fsck
Section titled “git fsck”fsck stands for “file system check.” It verifies the integrity of every object in your repository and reports anything broken or orphaned:
git fsck --fullBeyond catching corruption, it has a wonderful side effect: it lists dangling commits, commits that aren’t reachable from any branch or tag. If you’ve truly lost a commit and the reflog didn’t help, git fsck may surface it so you can recover it:
git fsck --lost-foundgit gc
Section titled “git gc”gc (“garbage collection”), which we introduced in Chapter 12, compresses loose objects and removes ones that are no longer needed. Run it when your repository feels bloated or sluggish:
git gcA calm troubleshooting mindset
Section titled “A calm troubleshooting mindset”Whenever Git surprises you, work through these steps before doing anything drastic:
- Read the message slowly. Git usually suggests the exact command to run next.
- Run
git status. It tells you where you are and what state you’re in. - Check
git reflog. It shows where you’ve been, which makes most “lost” work findable. - Avoid destructive commands (
reset --hard,push --force) until you understand the situation.
With that habit, the scary messages stop being scary and start being instructions.