Made a typo in your commit message? You can alter the order commits apply by changing the order of commits before saving. The first test is running the reset command with —hard flag. In this case, rewriting your branch's history is undesirable and you should instead revert this commit: git revert git push origin branch. You could manually create a commit with a message like that, but commit --squash saves you some typing. So, you may use the reset command to revert back the last commit or back to the specified state. You should definitely understand what you're doing before applying it! In this scenario, 0835fe2 and 6943e85 will be combined into one commit, then 38f5e4e and af67f82 will be combined together into another. Method 2: Undo the last commit message example As with every commit, a message is also saved. Mentioning the index doesn't help, we don't really know what that means yet. And sourcetree won't let me overwrite them. If you do small commits with only a fix, it will be easy in the future to revert or detect an error for example when using bisect.
How to undo last commit in Git. There will be different scenarios when you require undoing a commit in Git. In this tutorial, you may find different ways of undoing the previous commit. So, you may use the reset command to revert back the last git remove last pushed commit or back to the specified state. So, your last commit is undone and any files added or changes made are removed. Keeping the changes in previous commit example Basically, the reset command has three forms of invocation. The one is used in the above command i. So, for example, you do not want to discard changes made: files added, or any other changes. Still unclear, let us go through adding files in a branch, committing the changes and then doing hard and soft operations for undoing changes in the example below. The example of undoing commit in Git To understand the undo process by using Git restart with —hard and —soft flags, let us start with the basics. For the demo, I have created a remote and local repository. The remote repository is created on Github and local on a Window system. By right clicking on the local system folder, I opened the Git Bash. Fast forward, I ran a few commands as shown in the graphic below — for synchronizing the remote branch with local, creating a new branch locally and started adding files: Most probably, you already know about all that, this is just for beginners others may skip to next heading below. For learning how to undo commits in Git, I have git remove last pushed commit two more text files tst2. Running the reset command for undoing the last commit Finally, we are reached to the point where I will execute the reset command. The first test is running the reset command with —hard flag. If you go to the local repo folder, you will see the tst3. This is because of the —hard flag with reset command. So, you have to be careful as reverting back to last commits. You may lose your important files if undone carelessly. I will show what happens if we used —soft flag. For the example, I am again adding the third file tst3. Using the —soft flag for undoing last commit example Now let us go through using the —soft flag in reset command for undoing the last commit. For that, I am again adding three files and performing the commits one by one for each change as in above section. After that, we git remove last pushed commit three files in our demo1 branch in the local repository. The three files are also visible in the local repo folder. I again ran the status command. See what are the output of all the commands on Git Bash: You can see, the second status command shows new file: tst3. Also, as I checked the local repo folder, it shows all files tst1. Conclusion On the basis of above experiment, we may conclude that if you require to undo the last commit when you are sure to discard everything associated with that commit, then use the reset command with —hard flag. If you require keeping the changes like holding the code files or any other changes then use the reset with —soft flag. We also saw, not only you may undo the last commit by reset command but may go to any level of commits i. Method 2: Undo the last commit message example As with every commit, a message is also saved. In the case when you only require changing the message in the last commit, you should not use the reset command to go through all process again. Instead, you may use easier option — commit with —amend flag. Demonstration In the following example, I have added a text file in the local branch. This is followed by using the commit command with a message. I also used push command with —force flag for submitting the changes in the remote repository. This is how the message is fixed. Method 3: Using the revert command for undoing commit Another way of undoing the commit last or any is using the revert command. In this way, you have to provide the commit id in the revert command. The following commands show reverting back the last commit and any other specified commit by revert command. For the example, I have added another file tst4. After entering a message, press the following keys one by one : Esc : w q And press Enter. After that, it is showing only three files, thus the commit of tst4. Get the code there for the required commit that you want to revert.