How to Undo the Most Recent Local Commit in Git


Scenario A: Undo Latest Local Commit and Keep File Changes
1
$ git reset HEAD~1

The above command will:

  • Undo the most recent local commit.
  • Your local changes of the files are still there.
  • You need to add the changes and commit again.

Note: HEAD~ is the same as HEAD~1.

Scenario B: Undo Latest Local Commit and Delete Local Changes
1
$ git reset --hard HEAD~1

The above command will:

  • Undo the most recent local commit.
  • Your local changes of the files are Deleted.
Scenario C: Undo Latest Local Commit but Keep File Changes and Git Index
1
$ git reset --soft HEAD~1

The above command will:

  • Undo the most recent local commit.
  • Your local changes of the files are still there.
  • Your git index are still there – you do not need to git add the changes again.




Related Posts

What is the Difference Between Git Pull and Git Fetch

Short answers to the question what is the difference between...

How to Revert a Git Repository to a Previous Commit

Short answers to the question how to revert a git...

How to Modify Existing Unpushed Commit Messages

Short answers to the question how to modify existing unpushed...

How to Undo a Git Add Before Commit

Short answers to the question how to undo a git...

How to Rename a Local Git Branch

Short answers to the question how to rename a local...

How to Delete a Git Branch Locally and Remotely

Short answers to the question how to delete a git...