Git Amend

Continuing with my previous blogs on git rebase and interactive rebasing, I thought I’d write up a quick one on another git command I’ve been using more the last month or so.

git commit --amend

This special command allows you to update your last commit and rewrite history.  So instead of performing an interactive rebase and rewriting history for all commits on your current branch, you’re only doing so on the last commit.

For instance, let’s say you had a typo and just wanted to update your last commit message.  You can do so, by running the git amend command with the “-m” option, which allows passing a new commit message to rewrite history with:

git commit --amend -m "Update the last commit message to whatever you need"

Or let’s say you needed to stage additional changes in your last commit, such as updating an existing file, adding a file, and/or deleting a file.  You could run the same exact command as above.  The only difference is that since you’ve included staged files in the commit, those will now be picked up and included in the amend operation:

git commit --amend -m "Same command, but now include staged updates too."

So really, we’re using the same git command in the two examples.  The difference is that in our first example, there were no staged changes added to the git command, therefore its really only updating the commit message on the history rewrite.

Now let’s say you have another scenario where you have staged changes, but you wanted to keep the commit message the same.  For that, you can use the “–no-edit” option.

git commit --amend --no-edit

This will include those staged changes in the commit, but you wouldn’t need to include an updated commit message since the previous one would be used.

You could theoretically use the “–no-edit” option if there were no staged changes to be made, but that would really be odd because you’re rewriting history with the same changes and commit message as before.  So in essence not making any real changes.  Not saying you would, but for some reason if you wanted to, you could.


Below is a trivial example of using the git amend command:

  • Initial repo where I added two commits, each commit is adding a new sql file.Screenshot_061918_010238_AM.jpg
  • Now let’s say I added a new commit which includes yet another sql file.  But this time, I noticed I misspelled a word in the commit message.Screenshot_061918_010748_AM.jpg
  • Lucky me, I can go ahead and use the git amend command.Screenshot_061918_011453_AM.jpg
  • After doing so, you can now see that the last commit has been updated with the typo fixed on the commit message.  Nice!Screenshot_061918_011757_AM.jpg

 

  • On our next example, we’ll continue to make another new commit.Screenshot_061918_012149_AM.jpg
  • But this time, let’s say we forgot to include an additional file, script 5, to the last commit.  No worries, we’ll just perform the amend command to also include the staged change for the new script 5 file.Screenshot_061918_012941_AM.jpg
  • And boom, after the amend command, you can see that the last commit has been updated with the new commit message as well as picking up the staged file to be included in the commit, so now that commit includes Script 4 and Script 5.  Sweet!Screenshot_061918_012847_AM.jpg

Hopefully with the above examples you can see how powerful the amend command can be, allowing you to get other work done and worrying less about how nice and perfect each commit message needs to be.  Go ahead and use a quick commit message because you know you’ll always be able to update that commit.  Just remember, amend only applies to the last commit performed.

If you want to rewrite history further back than just the last commit, lucky you, I have just the blog for that type of interactive rebasing!


In conclusion, git’s interactive rebase has a little brother, amend, who lets you rewrite history only on the last commit performed, making it a nice command developers can use without worrying about having to perform a full interactive rebase.  So it’s less menacing and a more approachable alternative to getting developers used to rebasing and rewriting history.

So if I were to somehow make the comparison to basketball, it would be to use the amend command after game 5 of the 2018 Western Conference Finals, when the Houston Rocket’s Chris Paul injured his hamstring.

So being able to rewrite history on that one game so that he doesn’t injure his right hamstring vs. the whole playoffs is something we Rockets fan really wish could’ve been done.  Because if so, I do feel with a healthy CP3, we win that series and go on to…there I go again.  Trying to use git commands in the real world, Mutombo has something to say, “No, no, no”:

tejay van garderen basketball GIF

Leave a comment