Git

How to Undo “git commit –amend” Done Instead of “git commit”

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:

$ cd "C:\Git\features"

Step 3: Generate New File

Next, utilize the “touch” command and generate a new file:

$ touch Demo.txt

Step 4: Add File to Tracking Index

After generating the file, add the untracked repository changes to the tracking index:

$ git add .

To confirm whether the changes are tracked or not, utilize the “git status” command:

$ git status

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:

$ git commit -m "New Changes are committed"

Check the Git log to confirm whether the changes are committed or not:

$ git log

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:

$ git commit --amend

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:

$ git log

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”:

$ git reflog

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:

$ git reset --soft HEAD@{1}

Use the “git log” command to verify if we have undone the “git commit –amend” or not:

$ git log

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.