The tutorial discussed below provides with all the necessary skills that are required to work with a software project. In this tutorial we shall learn about undoing the last Git commit. At any stage you may arrive with a query in mind of undoing something. This is a human tendency which makes up the mind to do or not to do something. But what if you have done something but now you realize that you don’t need it or in simple words just want to undo something, you don’t need to worry because this tutorial takes you down with a simple series of steps to undo the commit. One needs to be careful because reverting back these undo’s is a bit difficult for the common man. There are some areas in the Git where you may lose something if you do it wrong. Now we come down to how to change the last commit.
The common undo takes place when someone like you commits too early or sometimes possibly forgets to add on some files, or may even mess the commit message. If you want to try the same commit again, you may easily run with the –amend option:
$ git commit --amend
The above command takes your enactment area and uses it for the commit. If you haven’t made changes since the last commit (instantly, you run with this command immediately after that previous commit), then the snapshot will look the exactly same and what all you will change is the commit message. Suppose the same commit message editor fires up, but it contains the message of that previous commit. One can easily edit the message the same way as always, but what is changed over here is that it overwrites with your previous/ last commit. For an example to the same, if someone like you commit and the only realize that you have forgotten to stage up the changes in a file you desire to add up to the commit, you may even do like the below shown
$ git commit -m initial commit $ git add forgotten_file $ git commit –amend
After these 3 commands what you end up with is a single commit where the second commit replaces the results of the first one.
The Git revert command helps in undoing with the committed snapshot. Here you may even discover a faulty commit too. When you discover that faulty commit, reverting is the safest and the easiest way to completely remove it from code base.
The Git reset command helps in undoing the changes to the files in the working directory. The resetting lets you to clean up or remove the changes that have not been pushed towards the public repository. Similarly, the Git clean command helps in removing the untracked files from the working directory. This is the logical counterpart towards resetting the Git that operates only on the tracked files.
Hope that you may have understood with how to undo the Git commit. Thanks for your valuable time.