Each important change made by the programmer can significantly affect the functionality of the project. Especially if it is developed in one environment, and then tested in another environment consisting of several virtual machines. Sometimes you can make changes (commits) to your Git repository, but then you realize that you made a mistake. Then you need to fix them in your project using the command line to undo the last commit in Git and make your code work again.
Commits as a Timeline
First of all, it is important to note that Git does not have a traditional “cancel” system, as in a text editor. In addition, he has his own nomenclature for operations of this type. This nomenclature includes terms such as reset, revert, checkout, clean, etc. If you think of Git as a timeline management utility, commits are snapshots of a point in time or a point on the project history timeline. Thus, to see the last commit is how to go on a time travel. In addition, with the help of branches you can manage several time frames.
Using the git log command
The whole idea of any version control system is to keep copies of the project so that you never have to worry about a fatal error in the code. It often happens that the change made does not have the expected result. Sometimes you need to see the changes and analyze the behavior of the project before and after the last commit. After you have created the history of the project, you can look at any commit in it. When you need to delete the last commit in Git, one of the best utilities for viewing the repository history is the git log command. For example, git log --oneline displays a list of commits in a branch.
Hash check
One way to undo the last commit in Git is to use a hash. You can get the hash using the git log command. Before doing this, you need to select the desired branch with the changes. In order to undo the last commit in Git, you will need to select the correct branch, but it is not always clear how to do this. But in Git, it's pretty simple. Each commit has a unique SHA-1 hash identifier. By default, only commits for the selected branch will be displayed in the git log. It is possible that the one you are looking for is on a completely different one. You can view all commits on all branches by running the git log --branches = * command. In order to verify a specific commit in Git, which, in your opinion, was the last working version, you can enter the following: git checkout <commit hash>. Such an operation will cause the working repository to correspond to the state of this commit. After analyzing a specific commit in Git, if you then decide to stay in that state, you can also cancel the last commit.
How to undo an erroneous commit
If you want to undo the last commit, you can use the <commit hash>, which you get from the git log command, and then write this command: git revert <commit hash>. She will create a new commit with the word Restore at the beginning of the message. After that, if you check your repository status, you will notice that you turned off HEAD in the commit that you tested earlier. But you hardly want to see this message. Therefore, to correct the situation and connect HEAD to the working repository, you should check the branch you are working on and enter the command: git checkout <current branch>.
The difference between git reset and git revert
There are other ways to undo the last commit in Git. The git revert command cancels it, creating a new one that represents the earlier state of the repository. But there is also git reset. It is important to understand the difference between git reset and git revert when using Git. The git reset command cancels the commit, removing the previous ones from the repository. It also reloads Git HEAD to an earlier commit, as described above.
How to choose a command to discard changes
Depending on which option is used, some Git stories may be lost. The git revert command cancels the commit, creating a new one that represents the earlier state of the repository. This will not result in a loss of Git history. So, it’s important to remember that there are several ways to “cancel” in a Git project. The git reset option is best used to undo local private changes. And git revert is the best tool to undo general public changes. Each of these teams has its own detailed documentation, which can be found separately. Keep in mind that once changes have been made, they usually remain constant, so seeing the latest Git commit is a useful feature.