DEV Community

Cover image for How to Undo Common Git Mistakes Without Panic
Shrestha Pandey
Shrestha Pandey

Posted on

How to Undo Common Git Mistakes Without Panic

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

  • --soft removes the commit and keeps your changes staged.
  • --mixed (the default) removes the commit and keeps your changes, but unstaged.
  • --hard removes 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Next, move main back to match the remote version:

git reset --hard origin/main
Enter fullscreen mode Exit fullscreen mode

Last, switch to your new branch:

git switch feature
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Find the commit you want back, and create a branch from it:

git branch recovered e4f5a6b
Enter fullscreen mode Exit fullscreen mode

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:

  1. Commit often. A commit is a safety point, because uncommitted work is the hardest to recover.
  2. Check before you delete. Run git status and git diff before commands like reset --hard or restore.
  3. Make a backup branch. Before a risky command, run git branch backup.
  4. Avoid force push on shared branches. If you must, use git push --force-with-lease. It refuses to overwrite work you have not seen.
  5. 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)

Collapse
 
suppdevbot profile image
DEV SUPPORTS •

You need to verify your account.

Enter fullscreen mode Exit fullscreen mode

tr.ee/dev-to