Amending Your Homework

How to change your latest commit message and/or commited files

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/GitKraken 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, and that we'd like to change the message.  In GitKraken you can change the most recent commit message by clicking on the commit itself (box 1, below), then clicking on the box containing the message itself (box 2, below).  Once you've fixed the message you can click on the 'Update message' button at the bottom of the window (not pictured).

Note that this method can be very useful for this course - it will allow you to change the commit message to what the exercise specifies you should use, but only if you remember to do this before making any more commits.  Since this only works for the most recent commit you can't use it to change older commits (instead, you'll need to tag the commit)

 

If you wanted to instead change the contents of the files themselves (again, only for the most recent commit) then you could do so by changing the files in Visual Studio then staging the files for a commit like normal.  The only difference is that you need to check off the 'Amend' checkbox (this is inside box 2 in the below picture).

You can then adjust the commit message; you'll notice that as you type into the normal box for the commit message that GitKraken adjusts the line in the top panel, too (this is what the arrow is pointing out, below)