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.
$ git commit --amend
$ git commit --amend -m "New commit message"
$ 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!