How to amend your last commit in Git repository?
This tutorial describes, how to amend your last commit description and author details in Git repository.- Article authored by Kunal Chowdhury on .
This tutorial describes, how to amend your last commit description and author details in Git repository.- Article authored by Kunal Chowdhury on .
There could be various reasons when you would like to amend your last commit. One of them is improper commit message and/or wrong committer information. In any of the case, you may like to change the previous commit.
In this small post, we will learn how to amend your last Git commit message as well as the committer information (username and email). Continue reading to know more.
Git allows you to rewrite its commit history using the command 'git commit --amend', but it actually does not modify the previous commit, instead it rewrites the entire commit snapshot to repository. Before amending your previous commit, make sure that, no other user has pulled your last committed changes.
In case, your original Git commit was pulled by someone and then if you rewrite the commit history, it will actually override the previous commit and thus the pulled author will not find the actual reference of the changed content. Hence, it is advisable not to amend a commit if you already pushed it to public repository. Please find below various cases and commands to perform this operation.
Amend a commit giving user a choice to update the commit message:
$ git commit --amend
Amend a commit replacing the previous commit message:
$ git commit --amend -m "New commit message"
Amend a commit with author details:
$ git commit --amend --author="Author Name <Author Email Address>"
I hope, you liked the post. If you came to this page by searching online about git basics or git tutorials, please have a look into my other blog posts. Subscribe to the RSS feed or the email newsletter to keep yourself updated. If you have any queries, you can either ping me on Twitter or drop a line below in the comments section. Have a great day!
Solution Architect & Former Microsoft MVP
Kunal Chowdhury is an enterprise solution architect and former multi-year Microsoft MVP. He is the author of three technical books: Windows Presentation Foundation Development Cookbook, Mastering Visual Studio 2017, and the Mastering Visual Studio 2019.
He publishes technical and non-technical articles on Kunal-Chowdhury.com.