Fun with the upcoming 1.7 release of Git: rebase --interactive --autosquash
The upcoming Git 1.7 has a lot of really nice improvements, and new features. One of the big new features is the --autosquash argument for git rebase --interactive.
If you’re anything like me, then you commit a lot, while you’re working on something, and use git rebase --interactive judiciously to clean up all these incremental commits into a presentable format. If you’re a bit more like me, then you’ll often end up doing multiple git rebase --interactive passes to split commits apart, and squash them back into other commits.
Git just gained the ability to make this a little faster. If you know what commit you want to squash something in to you can commit it with a message of “squash! $other_commit_subject”. Then if you run git rebase --interactive --autosquash commitish, the line will automatically be set as squash, and placed below the commit with the subject of $other_commit_subject.
If we run git rebase --interactive --autosquash origin/master from here, the pick-list will look like this:
When you get to the squash, you’ll have a commit message like:
If you were paying attention earlier to the pick-list, you’ll notice that there’s also a fixup command available. If we had specified fixup!, instead of squash! as the commit message’s prefix, then the pick list would have ended up as:
With the following in your editor for the combined commit message:
Notice that the fixup! commit’s message is already commented out. You can just save out the message as-is, and your original commit message will be kept. Very handy for including changes when you realize that you forgot to add part of an earlier commit.
Here’s a few aliases I have in my ~/.gitconfig setup to make all this easier:
Here’s how they would be used in our previous example:
Similarly, git fixup HEAD~2 would create a fixup! commit to be used with git rebase --interactive --autosquash (Aliased as: git ri).
Edit 2010-02-13: Fix alias examples.
Edit 2011-12-11: Change ‘Foo’s to “Foo”s to avoid grammar confusion.