Amending Your Homework

Let's say that you did everything required for a particular git-based exercise, you committed your work (i.e., you saved your work into git) and THEN you realize that you used the wrong commit message.  This page will walk you through amending your commit so that it has the correct commit message.

First, a quick note - this process will only work on the most recent commit.  So if you do the commit and then immediately notice your mistake you can use this process to amend that most recent commit.  However, if you do the commit wrong, then do one or more commits after the the wrong commit, you will NOT be able to amend that earlier, wrong commit.

The reason why we can't amend earlier commits has to do with how git identifies things.  Git uses something called a 'cryptographic hash' to generate a unique number for each 'thing' that git wants to identify.  (If you're curious, git uses the SHA-1 hash algorithm.)  In other words, each thing - each file, each directory/folder, even each commit - has a unique (and very long - 40 digit) number that uniquely identifies it.  git gets this number by looking at the unique pattern of bits in each thing and using that to calculate the hash value (the 40 digit number).  The main thing is that if we were to change the commit message then the SHA-1 hash value (the 40 digit number) would also change, but everything else (including the other commits we made after the wrong one) are still all using the original SHA-1 hash value. 

Since git doesn't go through and change all the other commits, and since the other commits all use that SHA-1 value of the original commit, we therefore can't amend an earlier commit.  (Note: if you know more about git you can use something called rebasing in order to do this, but we're not going t look at that here)

We can amend the most recent commit if we want to.  We can do this by creating a new commit object (with the new, corrected message).  It's ok to do this because there aren't any commits after it that are trying to refer to the wrong one - the wrong is the most recent commit.
Further, git/SourceTree switches to using that new, correct commit and simply ignores the wrong one.  Any further commits are applied to our correct commit, and so the wrong commit may as well as not happened.

For more information about amending in git you might look at the section on undoing things in the Pro Git book (available online for free)

Let's say that we have a project (repo) open, and that we've made a mistake with our most recent commit's commit message, as shown here:

Bad commit

Notice that the commit message reads "This is the WRONG commit message - D'oh!!!!", which is clearly wrong.

In order to amend this message, we need to first switch to the 'Working Copy' (next to arrow '1' in the below picture.

How to amend

After you've done that you should click on the button in the lower-right hand corner labeled "Commit options" (as pointed to by the '2' arrow, above).  Within that list choose the 'Amend latest commit' option.  At this point you should see a dialog box asking you if you want to replace the previous commit's message.  You should click 'Yes'

replace previous commit's message?

At this point you'll see the commit message reappear in the textbox (as pointed to by the '4' arrow in the above picture)

You can then type in the correct message, click the Commit button in the lower-right hand corner like normal, and commit your amendment.

If you then flip back to the 'BRANCHES' panel you should then see that the most recent commit has your new, fixed message.