Which commits to squash when rebasing in Git

git rebase is a really powerful tool, but I always forget which commits to pick and squash. Suppose your commit history looks like this:

bd7bf7d this is the commit message I want to keep
2313327 checkpoint commit 2
d4ac9ee checkpoint commit 1
c56d9c3 latest commit on master (don't change this one)

You want to run git rebase -i HEAD~3, which will bring up your editor:

pick d4ac9ee checkpoint commit 1
pick 2313327 checkpoint commit 2
pick bd7bf7d this is the commit message I want to keep

# Rebase c56d9c3..bd7bf7d onto c56d9c3 (3 command(s))
#
# 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
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Change the second two picks to s or squash. The editor should look like this:

pick d4ac9ee checkpoint commit 1
squash 2313327 checkpoint commit 2
squash bd7bf7d this is the commit message I want to keep

# Rebase c56d9c3..bd7bf7d onto c56d9c3 (3 command(s))
#
# 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
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Save the file and exit. Now you need to fix the commit messages:

# This is a combination of 3 commits.
# The first commit's message is:
checkpoint commit 1

# This is the 2nd commit message:

checkpoint commit 2

# This is the 3rd commit message:

this is the commit message I want to keep
[...]

Delete everything up to "this is the commit message I want to keep". Save and quit your editor. Now the rebase will proceed. Your commit history should now look like

5151034 this is the commit message I want to keep
c56d9c3 latest commit on master

Note the latest commit has a new hash - it's a new commit, created out of the three previous commits, which are now gone. So use it carefully!

Happy coding!