Think Like a Git, Commit Like a Boss: Reset and Amend

There are situations where you commit files to Git and discover that what you just did was very wr0ng.

There are also situations where your commit message is either wrong or has a typo.

Let’s see how we can handle such situations.

From our previous post, let’s go ahead and run a git status:

We should have something like this:


So we have just one commit:

Now, open the git-education folder, let’s add some information to the notes.txt file.

Add this:

Now the contents of notes.txt will be:

Let’s save and commit:

Now check the history, run git log, we’ll have something like this:


Now we have those,..but we just made a grammatical blunder by adding informations instead of information.

There are different ways to handle this, one way to this is to erase the last commit totally by doing this:

Reset your History

7227fde4b8d386c3937ab29c967123306dd9cb22 refers to the commit hash

So this totally erases all the changes made after the commit at 7227fde4b8d386c3937ab29c967123306dd9cb22

Now, open the notes.txt file, you will discover our last change to the file is gone totally. I don’t recommend this method..because you don’t absolutely need to do that just because you want to change a commit message. Imagine if you worked on a feature for about 1 hour, you don’t want your efforts gone in a jiffy.

Although this command is also very useful, you might be working with a team of developers that have just done or committed very wrong things to the project. The command above comes in handy because it just totally erases all their changes and cleans the  git history too to an earlier point in the course of the project.

Replace Last Commit

The best way to go about this is just running this command:

This checks git history for the most recent information and correct it to the commit message we have above. So simple!!!.

Go ahead and check the history using git log. You will discover that the history now contains the right commit message for the second commit.

Screen Shot 2015-08-18 at 8.18.15 PM

Now, we know this, there’s more to be learned!. Stay tuned for more posts on this series.

Please let me know if you have any questions in the comments section :smile:



Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.