fbpx

How to undo the most recent local commits in Git?

If you’ve ever wanted to undo the most recent local commits in Git before pushing your changes to production, you’ve come to the right place. There are two options to do this; both options reset the Git repository’s branch but one discards all of the changes while the other leaves the changes staged.

Option 1: Discard All of the Recent Commit’s Changes

The following command resets the Git repository’s branch one commit backward. The --hard parameter means that any changes to tracked files in the working tree since the last commit are discarded. The HEAD^ parameter tells git to reset the branch to the last commit.

git reset --hard HEAD^

Option 2: Stages the Recent Commit’s Changes

The following command resets the Git repository’s branch one commit backward. The --soft parameter means that any changes to tracked files in the working tree since the last commit are staged and are changes that are ready to be committed. The HEAD^ parameter tells git to reset the branch to the last commit.

git reset --soft HEAD^

After adding files or making changes to files, you need to run the command below to add the changes to your branch:

git add .
git commit -am "new changes"

Leave a Reply

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