In Progress
Unit 1, Lesson 1
In Progress

Git Storytelling – Part 3

Sometimes, we realize that something was just a *little* bit wrong in that last git commit. In this third and final entry in Brooke Kuhlmann’s guest series on curating our git history, he introduces us to the `amend` command. You’ll see how to avoid having to push an extra “whoopsie” commit to the project’s repo. Enjoy!

Video transcript & code

Episode III




In the previous episode, we used Git Interactive Rebase to reword, fixup, and squash our commits in order to prep our repository for sharing with fellow team members.

If we look at our Git history, the results of this endeavor looks great:


Before we consider this repository ready for sharing sharing with others, there is one last technique I want to leave you with...which is amending commits. I consider amending commits part of the Git Rebase Workflow, because when you amend a commit, you also change the SHA of the commit.

Let's say we realized, after rebasing, that our Ruby script was...too wordy:

ruby add.rb 1 2

What we want the script to output is the pure mathematical representation. So our last commit, which is our Ruby script implementation, isn't quite right. We could use Git Interactive Rebase to fix our last commit but that would be overkill. This is where amending a commit is the perfect tool for the job because amending only works on the very last commit.

The most basic usage is as follows:

git commit --amend

Notice I could have reworded the commit but didn't. This is a handy technique but it's the implementation we need to amend not the commit message.

To do that, let's first fix our implementation:

vi add.rb
git diff add.rb


ruby add.rb 1 2

...our script is producing the correct output so let's amend these changes:

git commit --amend --all --no-edit

So in a single command, I was able amend the implementation. The --all option allowed me to pick all file changes and the --no-edit option allowed me to skip having to edit the commit message altogether.

To confirm, you can see our Git status is clean:

git status --short --branch

...our Git history remains the same:


...and our implementation works as desired:

ruby add.rb 1 2

I hope you have enjoyed these Git Rebasing techniques and will add them to your workflow to keep your Git histories clean and descriptive in years to come.

Enjoy and Happy Hacking!