How To Undo Last Commit In Git?

Commits are used to put new versions to the source code. But some commits can be unwanted and required to undo the last commit. Also, wrong files can be committed accidentally which requires undoing the last command.

Undo the Last Commit (Soft)

The undo the last command can be done with the git reset command. There are two types of undoing the last commit. Soft Reset and Hard Reset . The soft reset reverts back to the last commit and preserves the files that do not require and are related to the current commit. The --soft flag is used for soft reset and HEAD~1 is used to specify the last commit.

$ git reset --soft HEAD~1

Undo the Last Commit (Hard)

The hard commit can be used to undo the last commit and delete all current commit files that clear the current working stage. The undoing last coming in a hard way can be executed with the git reset command like below.

$ git reset --hard HEAD~1

Undo To The Specific Commit

Alternatively, we can return to the specific commit by using the git reset command with the commit ID. Alternatively --soft or --hard options can be used for soft revert or hard revert.

$ git reset --hard a054dbc12

Leave a Comment