Git Autosquash: Committing code you want to squash during a rebase


Example

Given the following history, imagine you make a change that you want to squash into the commit bbb2222 A second commit:

$ git log --oneline --decorate
ccc3333 (HEAD -> master) A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 Initial commit

Once you've made your changes, you can add them to the index as usual, then commit them using the --fixup argument with a reference to the commit you want to squash into:

$ git add .
$ git commit --fixup bbb2222
[my-feature-branch ddd4444] fixup! A second commit

This will create a new commit with a commit message that Git can recognize during an interactive rebase:

$ git log --oneline --decorate
ddd4444 (HEAD -> master) fixup! A second commit
ccc3333 A third commit
bbb2222 A second commit
aaa1111 A first commit
9999999 Initial commit

Next, do an interactive rebase with the --autosquash argument:

$ git rebase --autosquash --interactive HEAD~4

Git will propose you to squash the commit you made with the commit --fixup into the correct position:

pick aaa1111 A first commit
pick bbb2222 A second commit
fixup ddd4444 fixup! A second commit
pick ccc3333 A third commit

To avoid having to type --autosquash on every rebase, you can enable this option by default:

$ git config --global rebase.autosquash true