Git log cleanup with git rebase -i

Save early, save often is good advice.

It's often not possible to finish a feature in one sitting or more complex features in even one day. I have two possible options when it comes to "save early, save often".

git stash or git rebase

git stash works fine, but I often forget to apply the stash when returning to a branch.

Use git rebase

git rebase has many uses, but I will focus on this one where `x' is the number of previous commits from HEAD.

git rebase -i HEAD~x

I can now save early and save often knowing that I can merge, or in git parlance, "squash" all the verbose commits into one commit later.

Let's go!

First, assign a default editor to git. I will use vim, but you can use others.

git config --global core.editor "vim"

Consider the following git log:

Planting a tree requires several steps. After the steps are complete, it's more useful to know what we have than how we got there.

For example, if I want to submit a pull request, the code reviewers will want to view the final code, not all the individual steps taken.

git rebase -i HEAD~3

This commands tells git I want to "interactively" rebase or squash the HEAD commit with the last 3 commits into one commit.

The documentation within each step is good, so read through that for the more details. Here I've replace 'pick' with r, s, s, and s.

Once you save and exit, the next file will appear for you. I used the 'r' command above, which allows me to 'reword' the commit message that summarizes the squashed commits.

Here's the revised log. Goodbye verbosity.