Git commit is an important command that is frequently used to commit changes before they are saved in the local repository of Git. It supports a wide range of parameters. For instance, using the “–amend” option with the Git “commit” command allows you to make changes to the commit in a text editor. Git users occasionally change the commits and then decide that the previous commit is preferable. In these cases, they want to undo “git commit –amend”.
This post will demonstrate the method for reverting the “git commit –amend” command instead of “git commit”.
How to Undo “git commit –amend” Done Instead of “git commit”?
The “git commit” and “git commit –amend” both commands are utilized to commit changes. By using the “git reset” command, Git users can undo or revert Git commits. For reverting “git commit –amend”, go through the given instructions.
Step 1: Open Git Terminal
Search for “Git Bash” in the Start menu and open the Git terminal from the search result:
Step 2: Open Git Repository
Move to the Git repository in the terminal using the “cd” command:
Step 3: Generate New File
Next, utilize the “touch” command and generate a new file:
Step 4: Add File to Tracking Index
After generating the file, add the untracked repository changes to the tracking index:
To confirm whether the changes are tracked or not, utilize the “git status” command:
It can be seen that the changes are successfully added to the staging area (tracking index):
Step 5: Commit Tracked Changes
Commit the tracked changes using and also embed the commit message using the “-m” option:
Check the Git log to confirm whether the changes are committed or not:
The below output indicates that changes are successfully committed with the added message:
Step 6: Edit Commit
Next, edit the message of the recent commit and utilize the “–amend” option to edit it:
Upon doing so, the default selected text editor will appear on the screen from where you can edit the commit, and press “CTRL+S” to save changes:
Again, check the Git logs to verify whether the commit is edited or not:
From the below output, you can see that the commit message is edited successfully:
Step 7: Open Git Log With References
Open the Git logs with references by utilizing the “git reflog” command. Note the commit reference (HEAD position) to undo “git commit –amend”:
Step 8: Undo “git commit –amend”
After that, undo the “git commit –amend” using the “git reset” command along with the commit reference/HEAD position. Moreover, the soft option is used to undo “git commit –amend” without destroying the related changes:
Use the “git log” command to verify if we have undone the “git commit –amend” or not:
The output shows that we have successfully reverted the “git commit –amend” operation:
You have learned how to revert the “git commit –amend” instead of “git commit”.
Conclusion
To undo the “git commit –amend” instead of “git commit”, first open the Git repository, and commit the tracked changes. Then, edit the commit changes using the “git commit –amend” command. Now, revert the “git commit –amend” instead of git commit utilizing the “git reset –soft HEAD@{1}” command. In this tutorial, we have elaborated on the method to undo or revert the “git commit –amend” command instead of the git commit.