My new favourite Git feature: git rebase –-autostash
UPDATE 14/01/16:
Git v2.7.0 is out! All you need to do now is set the following global config flag and you can simply rebase as usual even onto a dirty worktree: git config --global rebase.autostash true
UPDATE 27/10/15:
The recently released Git v2.6.0 has been taught to pay attention to your rebase.autostash configuration which enables the --autostash
option by default. That means that a simple git pull --rebase
is now capable of rebasing a dirty worktree.
ORIGINAL POST:
I recently stumbled upon a (in my opinion) less well-known feature of Git that’s actually been around since v1.8.4 (current version: v2.4.6).
It is the git rebase --autostash
command. And it basically does what it says: Automatically stashing away changes before performing the rebase operation. Before that release, rebase would just refuse to run.
It is the equivalent to: git stash & git pull --rebase & git stash pop
.
Now think about how many times you’re doing that per day. At least I find myself executing those commands over and over again to stay up-to-date with the remote origin.
So I created a handy alias for git fetch && git rebase --autostash
, so I only need to run git update
.
If you’re using SourceTree, you might also want to create a custom action that allows you to do the same operation with a single mouse click. Just create a new file called stash_and_rebase.sh
with the following contents:
#!/bin/sh
git fetch && git rebase --autostash
You can then set it up via SourceTree / Preferences / Custom Actions / Add like so:
Baam! That’s it. And it saves me a couple of seconds every day :)