Skip to content

How to Undo the most recent local commits in Git

If you have accidentally committed the wrong files into Git, but haven’t yet pushed it to the server, you can recover, or undo your commit as follows:

How to Undo a Commit and Redo it

You have just committed a file, and find yourself here;

We will call this “your accident location”.

Step 1

git commit -m "This is what you've just done"
Code language: Bash (bash)

Step 2

Start by resetting:

git reset HEAD~
Code language: Bash (bash)

Step 3

Now you can edit your files as required.

Step 4

When you are done, run the following commands:

git add . git commit -c ORIG_HEAD
Code language: Bash (bash)

A note on Step 2

Step 2 said to use git reset HEAD~

Note that HEAD~ is the same as HEAD~1, which can allow you to undo a certain amount of commits. As we only stipulated 1 or simply ~, it only undid 1 place.

Swap this out for a greater number if you want to undo a larger amount of commits.

See also  [Solved] Error - The following untracked working tree files would be overwritten by merge
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x