Before you submit a pull request to the upstream repository for changes you made in your Git repository, you should think about cleaning up your commit history. Ideally, when your code is merged with the master branch of the target repository, the commit history of that master branch should be succinct and easy-to-follow, and it should reflect a clean progression of code changes.
Leave small insignificant commits out of the final commit history. In other words, the master commit history should not contain commit messages like, "Test a new function" or "Begin work on X." These kinds of messages pollute the commit history and makes it difficult to find where a major change started and ended. If there were many small commits that led to a significant change, there should be one commit that encompasses all those small commits.
So how do we go about cleaning up the commit history before merging with master? We can use git rebase -i
. You can use this command to do a variety of tasks like reordering commits, deleting commits, updating commit messages, combining commits, and more. This post will focus on combining commits, or squashing them.
Assume we have the following commit history (shown with the latest commit first) and we want to combine the last 3 commits:
408xcad Return result
48copd7 Calculate square root
8cj3x7z Check input type
pl707da Add functionality for calculating the square of a number
We will type the following in the shell:
git rebase -i HEAD~3
This will bring up your default text editor and will show the following:
pick 8cj3x7z Check input type
pick 48copd7 Calculate square root
pick 408xcad Return result
# Rebase pl707da..408xcad onto pl707da
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
The oldest commit will be at the very top. Since we want to combine these three commits into one, we will update the lines like so in the text editor:
pick 8cj3x7z Check input type
squash 48copd7 Calculate square root
squash 408xcad Return result
# Rebase pl707da..408xcad onto pl707da
Save and close the editor. Git will then automatically launch the editor again, and this time, you can specify the commit message of the combined snapshot. The initial prompt will be something like this:
# This is a combination of 3 commits.
# The first commit's message is:
Check input type
# This is the 2nd commit message:
Calculate square root
# This is the 3rd commit message:
Return result
Since we are squashing the 2nd and 3rd commits into the 1st commit, we'll update the commit message of the first message like so:
# The first commit's message is:
Add functionality for calculating the square root of a number
Now save and close the editor. Then check out your commit history using git log
and you should see the following:
6c8a3ll Add functionality for calculating the square root of a number
pl707da Add functionality for calculating the square of a number