Everyone makes mistakes in Git. Like committing to the wrong branch, writing a bad message, or deleting something by accident. The good news is that Git keeps a lot of history, so most mistakes can be fixed.
In this post, I will go through seven common mistakes and the commands that fix them. For each one, I will say whether the fix is safe or if it can delete work.
1. You wrote a bad commit message
If you have not pushed the commit yet, you can change the message of the last commit:
git commit --amend -m "Better message here"
This replaces the last commit with a new one. If you already pushed the commit, avoid this. Changing pushed history causes problems for other people on the team.
2. You committed too early
Suppose you made a commit, but you forgot a file, or you want to change the code a bit more. Undo the commit and keep your changes:
git reset --soft HEAD~1
This removes the last commit, but your changes stay staged, so you can edit and commit again.
Here is how the three types of reset differ:
-
--softremoves the commit and keeps your changes staged. -
--mixed(the default) removes the commit and keeps your changes, but unstaged. -
--hardremoves the commit and deletes your changes. Be careful with this one.
3. You staged a file by mistake
You ran git add on a file that should not be in the commit. Unstage it:
git restore --staged filename.js
The file stays changed on your disk. It is just no longer staged. This is safe.
(In older versions of Git, you may see git reset HEAD filename.js for the same task.)
4. You want to throw away changes in a file
You edited a file, and now you want it back to the last committed version:
git restore filename.js
Warning: This cannot be undone. Git has no copy of changes you never committed or staged. Check with git diff first if you are not sure.
5. You committed to the wrong branch
This often happens when you forget to create a new branch. Say you made a commit on main, but it should be on a new branch called feature.
First, create the new branch at your current commit. This keeps the commit safe:
git branch feature
Next, move main back to match the remote version:
git reset --hard origin/main
Last, switch to your new branch:
git switch feature
Your commit is now on feature, and main is clean. Note that git reset --hard deletes any uncommitted changes, so commit or stash them first.
6. You pushed a bad commit
When a commit is already pushed and shared, do not rewrite history. Use revert instead:
git revert <commit-hash>
This creates a new commit that does the opposite of the bad one. The history stays honest, and nobody's work breaks. This is the safe way to undo shared work.
You can find the commit hash with git log --oneline.
7. You lost commits or deleted a branch
This is where reflog helps. Git records every place HEAD has been, even after a hard reset or a deleted branch:
git reflog
You will see a list like this:
a1b2c3d HEAD@{0}: reset: moving to HEAD~2
e4f5a6b HEAD@{1}: commit: Add login form
c7d8e9f HEAD@{2}: commit: Add user model
Find the commit you want back, and create a branch from it:
git branch recovered e4f5a6b
Your work is back on a new branch called recovered.
Reflog entries do not last forever. By default, Git keeps them for about 30 to 90 days, so recover lost work soon after it happens. Also, reflog only tracks commits. It cannot bring back changes that were never committed.
A few safety habits
These small habits make mistakes less painful:
- Commit often. A commit is a safety point, because uncommitted work is the hardest to recover.
-
Check before you delete. Run
git statusandgit diffbefore commands likereset --hardorrestore. -
Make a backup branch. Before a risky command, run
git branch backup. -
Avoid force push on shared branches. If you must, use
git push --force-with-lease. It refuses to overwrite work you have not seen. -
Read the command first. If you do not know what a command does, check the docs with
git help <command>.
Quick reference
| Problem | Command |
|---|---|
| Bad last commit message | git commit --amend -m "..." |
| Undo last commit, keep changes | git reset --soft HEAD~1 |
| Unstage a file | git restore --staged <file> |
| Discard changes in a file | git restore <file> |
| Undo a pushed commit | git revert <hash> |
| Find lost commits | git reflog |
Final thoughts
Git feels difficult at first, but it is built to protect your work. Most of the time, a commit you made is still there, even if it looks gone. When something goes wrong, stop, run git status and git reflog, and think before you run the next command.
Which Git mistake have you made the most? Tell me in the comments.
Top comments (1)
tr.ee/dev-to