How to amend your last commit in Git repository?


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.

 

How to amend your last commit in Git repository? (www.kunal-chowdhury.com)

 

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!

 

 

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments

 
© 2008-2017 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top