Skip to content

How to Remove the Last Commit in Git (Safely & Easily)

4 min read

If you’ve just made a commit in Git and immediately realized it was a mistake—don’t panic! Git offers powerful tools to help you undo your most recent commit without breaking your project. Whether you just want to remove the commit message, completely erase the commit, or keep your code changes intact, this guide will show you how to remove the last commit in Git safely and correctly.


🤔 Why Would You Want to Remove a Git Commit?

Some common reasons include:

  • You committed to the wrong branch
  • The commit message was incorrect
  • You forgot to include files
  • You want to squash commits for cleaner history
  • You just want to start fresh before pushing

Luckily, Git gives us the flexibility to handle all these situations.


🛠️ 1. Undo the Last Commit But Keep Changes (Soft Reset)

This is the safest and most commonly used way to remove the last commit.

bash

git reset --soft HEAD~1
🔍 What it does:
  • Removes the last commit
  • Keeps your changes staged (in the index)

This is perfect when you just want to modify the commit message or add more changes before committing again.

✅ Use Case:

“I made a commit but forgot to add one more file. I don’t want to lose anything, just redo the commit.”


🧹 2. Remove the Commit and Unstage the Changes (Mixed Reset)

If you want to keep the changes but unstage them (send them back to your working directory):

bash

git reset --mixed HEAD~1
🔍 What it does:
  • Removes the commit
  • Unstages changes (but keeps them in your files)

This is useful when you want to re-organize what to include in your commit.


🚫 3. Completely Erase the Commit and the Code (Hard Reset)

Dangerous but sometimes necessary. This will remove the commit and discard all code changes.

bash

git reset --hard HEAD~1
⚠️ WARNING:

You cannot recover the code easily after a hard reset unless you’ve backed it up or pushed it to remote.

✅ Use Case:

“I committed a huge mistake, and I just want to start from the previous state.”


🔁 4. Undo a Commit That Was Already Pushed (Use With Caution)

If you’ve already pushed the commit to a remote (like GitHub), things get trickier.

Option A: Revert the Commit (Safe)

bash

git revert HEAD

This creates a new commit that undoes the changes of the previous commit. It’s the safest approach for collaboration.

Option B: Force Push (Dangerous)

If you’re sure no one else is using the branch:

bash

git reset --hard HEAD~1
git push origin main --force
⚠️ Be very careful:

Force pushing can rewrite history and mess things up for your team.


🧠 Key Concepts to Remember
CommandKeeps ChangesUnstages ChangesRemoves Changes
--soft✅ Yes❌ No❌ No
--mixed✅ Yes✅ Yes❌ No
--hard❌ No❌ No✅ Yes

📚 Real-World Example

Imagine this situation:

bash

git add .
git commit -m "Fix typo in config"

A second later, you realize it was the wrong file, or you forgot to add something. You can fix this with:

bash

git reset --soft HEAD~1

Now re-add the correct file and recommit:

bash

git add correct-file.js
git commit -m "Fix typo in correct-file.js"

Clean, simple, and no harm done.


🛡️ Best Practices
  • Use soft reset when you’re just editing commits.
  • Avoid hard reset unless you really know what you’re doing.
  • Use git revert if you’ve already pushed your commit to a shared branch.
  • Always make a backup (stash or branch) before destructive commands.
  • Communicate with your team before rewriting history.

🧪 Bonus Tip: View Commit History

To see your last few commits before deciding what to remove:

bash

git log --oneline

This gives you a quick snapshot like:

sql

c3f1f72 Fix bug in navbar
7f9b7c3 Add contact form
2a1d5f0 Initial commit

You can replace HEAD~1 with any commit hash if you want to reset deeper.



🚀 Conclusion

Whether you want to undo a commit to fix a mistake, clean up your history, or revise your code changes, Git provides several tools to do it safely. Use git reset with the appropriate flags to match your needs, or git revert for collaboration-friendly changes.

Learning how to remove the last commit in Git gives you more control over your workflow—and prevents those little mistakes from turning into big problems.

👉 Ready to practice?
Try creating a test branch and experiment with git reset and git revert. You’ll be a Git pro in no time.

If you found this helpful, don’t forget to bookmark this post or share it with your fellow developers!