Help, I
Real Git mishaps and the commands that get you out of them. Every fix was run against real Git, and most lead straight into a level where you can practise it.
31 fixes
Getting started
- get “not a git repository”Git cannot find a repository in this folder or any folder above it. You are almost always just one
cdaway. - get “Please tell me who you are” when I commitEvery commit records an author, and Git does not know yours yet. Two config lines fix it for good.
- am stuck in Vim after git commit
git commitwithout-mopens an editor, often Vim. Here is how to save the commit or cancel it, and how to never land there again.
Commits
- committed to the wrong branchYour commit landed on main instead of your feature branch. Move it over without losing anything.
- made a typo in my last commit messageFix the message of your last commit with one command, and know when it is safe to push the change.
- forgot to add a file to my last commitAdd the missing file to the commit you just made, or follow up with a second commit if it is already pushed.
- made too many tiny commits and want to squash themTurn “wip”, “fix typo” and “wip 2” into one clean commit before anyone reviews your branch.
- committed a password or API keyRevoke the secret first, then get it out of your history. What that takes depends on whether you pushed.
- committed a file that should have been ignored
node_modules, build output or logs ended up in Git. Stop tracking them without deleting them from your disk.
Undo and recover
- want to undo my last commit but keep the changesTake back the last commit and keep every change in it, ready to fix up and commit again.
- want to throw away my last commit completelyDelete the last commit and its changes, or undo it with a new commit if you have already pushed.
- pushed a commit that broke everythingUndo one pushed commit safely with
git revert, without rewriting history your team already pulled. - lost commits after git reset --hardThey are not gone. The reflog remembers every place HEAD has been, so you can jump right back.
- deleted a branch I still neededDeleting a branch only deletes the label. Put it back on its last commit and everything is there again.
- want to throw away my changes to a fileGet a file back to how it was in your last commit and start over, for one file or all of them.
- staged a file by accidentTake a file out of the next commit without touching what is in it.
- deleted a file by accidentBring a deleted file back from Git, whether you deleted it a minute ago or in a commit last week.
- merged the wrong branchUndo a merge you did not mean to make, before or after pushing it.
Conflicts
- got a merge conflictBoth branches changed the same lines and Git wants you to decide. Resolve it in four steps, or back out.
- am stuck in a rebase with conflictsA rebase stopped halfway because of a conflict. Finish it, or go back to how things were.
- can't switch branches because of my local changesGit will not switch while your uncommitted work would be lost. Put it aside, switch, and bring it back later.
Branches and HEAD
- am in “detached HEAD” stateHEAD points at a commit instead of a branch. Keep what you did on a new branch, or simply go back.
- need the latest main in my feature branchmain moved on while you worked. Bring its new commits into your branch with a rebase or a merge.
- gave my branch the wrong nameRename a branch locally, and on the remote too if you already pushed it.
- tagged the wrong commitMove a tag to the right commit, locally and on the remote.
Push and pull
- had my push rejectedSomeone pushed before you did. Put your commits on top of theirs and push again, without force.
- can't push: “has no upstream branch”Your new branch does not exist on the remote yet. One push with -u creates it and links the two.
- can't pull: “You have divergent branches”You and the remote both have new commits, and Git wants to know how to combine them. Choose once, and the message is gone.
- get “refusing to merge unrelated histories”Your local repository and the remote started separately, typically with a README created on GitHub. Join them once.
Detective work
Missing the mess you are in? Suggest a mishap on GitHub