Programming & Coding How-TosTechnology

How to Undo Your Last Git Commit Without Losing Changes

Admin
How to Undo Your Last Git Commit Without Losing Changes

You just pressed Enter on a commit and instantly regretted it. Maybe the message has a typo, maybe you committed too early, or maybe you included a file that should not be there. Your first fear is that fixing the commit will throw away all your hard work.

Good news: it will not. Git gives you a safe way to undo the last commit while keeping every file change exactly as it is. This guide shows you how, step by step, with clear warnings about the options that are not safe.

Quick Answer

To undo your last Git commit without losing file changes, run git reset --soft HEAD~1. This removes the last commit but keeps all your changes staged and ready, so you can fix anything and commit again. If you want the changes kept but unstaged, use git reset --mixed HEAD~1 instead, which is also the default reset.

What “Undo a Commit” Really Means in Git

A commit is a snapshot with a message. Undoing a commit does not have to mean deleting the snapshot’s contents. With a reset, you simply move the branch pointer back by one commit, like moving a bookmark back one page in a book. The pages themselves, your file changes, stay right where they are.

The key question is what happens to those changes after the bookmark moves. That is decided by the mode you choose: soft, mixed, or hard. Picking the right mode is the whole skill here, because one of them keeps everything, one keeps files but unstages them, and one deletes the work completely.

The Three Reset Modes, Explained Simply

  • --soft: Undo the commit, keep changes staged. Your files look exactly as they did just before you committed. Best when you only want to redo the commit itself.
  • --mixed: Undo the commit, keep changes but unstage them. This is the default if you type no mode. Best when you want to re-pick which files go into the next commit.
  • --hard: Undo the commit and delete the changes completely. Dangerous. Only use this when you truly want to throw the work away.

For the question in this article, keeping your changes, you want soft or mixed. Never hard, unless discarding the work is exactly your goal.

How to Undo the Last Commit and Keep Changes, Step by Step

Step 1: Look at What You Are About to Undo

  1. Run git log --oneline -5 to see your last few commits in a short list.
  2. Confirm the top one is really the commit you want to undo.
  3. Run git status to check whether you also have newer uncommitted changes sitting on top. A soft or mixed reset will not harm them, but it is good to know they exist.

Step 2: Run the Soft Reset

  1. Type git reset --soft HEAD~1 and press Enter. There is no dramatic output, which is normal.
  2. Here HEAD means your current commit, and ~1 means one step back from it.
  3. Run git status. You should see all the files from that commit listed as staged changes, ready to commit again.

Your work is now exactly as it was just before you committed, so you can commit again with git commit -m "Your corrected message".

If you are still building confidence with everyday commands, keep our guide to essential Git basics every beginner should master nearby while you practice these steps on a test repository.

Step 3: Make Your Corrections

With the changes staged, you have full freedom to adjust things before committing again.

  • To add a forgotten file, use git add filename.
  • To remove a file from the staged set without deleting it from your disk, use git restore --staged filename.
  • To edit file contents, just edit and save them in your editor, then run git add on them again so the newest version is staged.

When everything looks right in git status, commit again with a clear message. You have now replaced one flawed commit with one clean commit, and no work was lost in the process.

Alternative: Keep Changes but Unstage Them

Sometimes you undid the commit because the grouping was wrong. You bundled two separate tasks into one commit and want to split them into two tidy commits. For that, un-staging is actually helpful.

  1. Run git reset --mixed HEAD~1, or simply git reset HEAD~1, since mixed is the default.
  2. Run git status. Your files are still changed on disk, but nothing is staged.
  3. Stage only the first task’s files with git add, commit them, then stage and commit the second task’s files.

The files never leave your disk in either mode. The only difference is whether they sit in the staged area waiting, or back in your normal working area.

What If You Only Want to Fix the Commit Message?

If the code in the commit is perfect and only the message is wrong, you do not need a reset at all. There is a shorter tool made exactly for this.

  1. Run git commit --amend -m "Your new message".
  2. Git replaces the last commit with a new one containing the same changes and your corrected message.

You can also run git commit --amend without the message part to open your editor, fix the message there, and even add forgotten files you staged first. According to the official Git commit documentation, amend rewrites the tip commit, which is why it is perfect for a last-commit fix and unsuitable for older shared commits.

What If You Already Pushed the Commit?

Everything above assumes the commit only exists on your computer. If you already pushed it to GitHub, the story changes, because other people may now have that commit.

After a soft reset and a new commit, a normal git push will be rejected, because history has been rewritten. You would need git push --force-with-lease to update the remote. Only do this on a branch that is yours alone. Never force push a shared branch like main that teammates are using, because it can overwrite their view of history and cause serious confusion.

For shared branches, the safer public undo is git revert HEAD, which creates a brand new commit that cancels the old one without rewriting anything. Revert is the polite, team-friendly option. Reset is the private, not-yet-pushed option. Choosing between them based on whether you pushed is one of the most important judgments in daily Git work.

Common Mistakes to Avoid

  • Using --hard when you wanted to keep changes. This is the number one way beginners lose work. Read the command twice before pressing Enter, especially any command containing the word hard.
  • Resetting commits you already pushed to a shared branch. Use revert for shared history. Save reset for your private, local commits.
  • Forgetting to check git log first. Undoing the wrong commit creates a second problem on top of the first one.
  • Panicking when there is no output. A successful reset is quiet. Silence means it worked, not that it failed. Check with git status and git log --oneline.
  • Thinking reset deletes files in soft or mixed mode. It does not. Your edits remain on disk in both modes. Only the commit record and, in mixed mode, the staging are undone.
  • Not committing again and then switching branches. After a reset, finish the job by making your corrected commit promptly so your work returns to a safely recorded state.

Conclusion

Undoing your last commit without losing changes is a ten-second job once you know the right flag. Use git reset --soft HEAD~1 to keep everything staged and ready, or git reset --mixed HEAD~1 when you want to regroup files before committing again. Avoid --hard unless deleting the work is truly your intention, and switch to git revert whenever the commit has already been shared with a team. Practice this once on a throwaway test project, and you will never fear a mistaken commit again.

Frequently Asked Questions

Will git reset –soft delete any of my files?

No. A soft reset only moves the branch pointer back and leaves your working files and staged changes completely untouched. It is the safest reset mode. After running it, git status will show all the changes from the undone commit still staged and ready to commit again.

What is the difference between reset –soft and reset –mixed?

Both keep your file changes on disk and both undo the commit. The soft mode keeps the changes staged, as if you had just run git add on them. The mixed mode unstages them, returning them to normal modified files, which is handy when you want to choose again which files belong in the next commit.

I used –hard by mistake. Can I get my commit back?

Often, yes. Git keeps a private log of where HEAD has been. Run git reflog, find the line for your lost commit, and run git reset --hard <that-hash> to return to it. The GitHub guide to undoing commits also covers recovery options. Act quickly though, because reflog entries eventually expire.

Can I undo a commit that is not the last one?

It is possible with interactive rebase, but it is more advanced and riskier, especially if the commit was pushed. For beginners, if the older commit was never pushed, an easier path is to soft reset back past it, fix things, and recommit in order. If it was shared, use git revert <hash> to cancel that specific commit safely.

Does undoing a commit change the commit date and hash?

Yes. When you commit again after a reset, Git creates a genuinely new commit with a new hash and a new timestamp, even if the file changes and message are identical. That is expected behavior, since Git treats it as a fresh snapshot rather than an edited copy of the old one.

Admin

Passionate about the intersection of style and storytelling. I simplify fashion trends and industry news into easy-to-follow advice, helping you stay stylish and informed in the fast-paced world of fashion.

Leave a Reply

Your email address will not be published. Required fields are marked *