This page looks best with JavaScript enabled

Git Commit Undo: Mastering Soft and Hard Resets for Local Commits

 ·  ☕ 3 min read  ·  ✍️ Dinesh Arora


Hey there, fellow developer! Ever committed something to Git and thought, “Oops, I need to take that back!”? We’ve all been there. The good news is that Git provides us with the tools to undo those local commits without a hassle. In this post, we’ll explore why you might want to undo local commits and walk through the steps, including soft and hard resets, with real code samples.


Imagine this common scenario: you’re working on your latest project, happily making progress and committing changes along the way. Then, suddenly, you realize that your last commit was a premature one. Maybe you included a file you didn’t mean to, or perhaps you discovered a bug right after committing. It happens, right?

That’s when the need to undo local Git commits arises. We want to rewind the clock and make things right. Let’s dig into how you can do that.

Use Case: Soft Reset

Soft reset is your go-to when you want to keep your changes from the undone commit staged in your working directory. It allows you to review and possibly modify the changes before committing again.

Here’s how you do a soft reset:

1
$ git reset --soft HEAD~1

In this command, HEAD~1 refers to the commit you want to undo. It moves the HEAD pointer back to the previous commit, effectively “uncommitting” your last change. Your changes remain staged and ready for editing.

Use Case: Hard Reset

On the flip side, there’s the hard reset. This one’s more drastic as it discards the changes from the undone commit entirely. You’d use this if you want to completely remove the last commit, including its changes, from your branch’s history.

Here’s how you do a hard reset:

1
$ git reset --hard HEAD~1

Be extra cautious with a hard reset because it permanently discards all changes from the last commit.

Let’s see these resets in action with some code samples.

Soft Reset

Suppose you committed a file called my-file.txt that you now want to uncommit but keep the changes staged.

1
2
3
$ git log
$ git reset --soft HEAD~1
$ git status

Now, the changes from the undone commit are staged, and you can make adjustments.

Edit my-file.txt to fix any issues.

1
$ nano my-file.txt

Commit the changes again.

1
$ git commit -m "Your revised commit message here"

Hard Reset

Imagine you committed a file called big-mistake.txt that you absolutely need to remove from history.

1
2
$ git log
$ git reset --hard HEAD~1

That’s it. The last commit, including all its changes, is gone forever.

When should you use Soft or Hard Reset?

Undoing local Git commits is a valuable skill, and the choice between a soft reset and a hard reset depends on your specific situation:

  • Use a soft reset when you want to keep the changes staged and ready for editing.
  • Use a hard reset when you want to completely remove the last commit, including its changes.

Remember, communication is key when collaborating with others. Always inform your team about any changes to the Git history. So, the next time you find yourself in an “Oops” moment, don’t worry. You’ve got the Git reset tools at your disposal to make things right.

Happy coding!

Share on
Support the author with

Dinesh Arora
WRITTEN BY
Dinesh Arora
Developer