How to undo the last local commit in Git repository

The Git is a very popular version control system, it's widely adopted and almost every professional developer has to know how to use it correctly. Open source web applications and other types of software are very often stored on the public Git repositories, the enterprise and private projects also use this version control system.

And, in such a situation, it's especially important for the developer to be able to solve various issues with the files or commits in the source tree.

This article describes a very specific case that may occur in the project that you work on. For example, you modified the source files, then committed those changes, and after that, you decide that you don't really want to push the files to the remote repository.

To resolve this issue, you may need to undo the last local commit in your branch. There are several ways how it can be done, and you need to choose a solution that is the most appropriate in your specific case.

Soft reset of a commit

You can reset a branch in a soft mode, this means that the index will not be touched. The files will be kept in the staging area and the HEAD will be moved to the target commit in accordance with the specification provided in the git reset command.

Also, if you have previously called the "git add" command, you do not need to call it again after resetting the Git repository in a soft mode.

Following is the example of how to undo only the last commit:

git reset --soft HEAD~

The "~" symbol, which is also known as "tilde", is an ancestry specification that refers to a first parent.

You can provide an exact number of commits to undo on a current branch. The equivalent of the previous command is shown below, there we specify that we want to reset only a single commit.

git reset --soft HEAD~1

Also, here is the example that shows how to reset last two commits:

git reset --soft HEAD~2

You can go as deeper in a commit history as you need, following statement shows how to reset three commits:

git reset --soft HEAD~3

Mixed reset of a commit

This option is useful if you also want to reset the index, which means that the files will also be removed from the staging area. In other words, in addition to the soft mode, the "mixed" mode will also revert the "git add" operation on a current branch.

However, modifications to the files are preserved and the working tree is kept unchanged after calling the git reset command.

Following is the example of how to undo the last commit in a mixed mode:

git reset --mixed HEAD~

Also, it is worth to mention that the "mixed" method is the default action that is chosen by the Git tool if there is no explicit mode set in the command line arguments. It means that the above example is an equivalent to the command given below:

git reset HEAD~

Which version to use is a matter of a choice. However, to make it more prominent which mode is selected, you may prefer to always set the "--mixed" option.

Hard reset of a commit

If you choose this option to undo a commit, then both the index and the current working tree will be reset.

It's important to note that changes made to the tracked files will be lost, so please be careful with performing this command. Optionally, you can make a backup of the working tree to be able to restore the files in case if something goes wrong during the reset operation.

See below how to specify "--hard" mode for the Git reset command.

git reset --hard HEAD~

Also, you can set the exact commit id in the command line, see how it can be done below:

git reset --hard <COMMIT>

Revert of a commit

This is another method that allows to undo your last commit. However, instead of discarding the changes, the "revert" option instructs the Git to create a new commit on a current working tree.

This option is useful if you want to keep the history of modifications in the repository.

Please see below how to perform such a task in the command line:

git revert HEAD

Conclusion

If you are actively working on a project and adding some features or experimenting, there may be times when you need to recover from the specific commit, and being able to do that in a very efficient way is a crucial skill.

As you have noticed, the Git version control system provides several mechanisms to manage the commit history, you may easily undo any change you've made in an active branch with the method that is the most suitable in your specific case.

Thank you for reading our article. If you have any questions or notes related to the topic described in this post, please leave them in the comment form below.

Leave a Reply

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