Git: How to automatically stash while rebasing or merging
Imagine you’re part way through making some changes, and you realize that you need to rebase or merge. You can:
- Save your work with
- Rebase or merge.
- Unstash your current changes with
git stash pop.
Since this is a useful sequence, Git has “autostash” options that make
merge automatically do the stash and unstash steps for you. Let’s look at them now.
How to rebase with autostash
Say you’re on the
$ git log --oneline 413145f (HEAD -> almond_butter) Blend almonds 1e6a6cd Roast almonds ...
…with some changes pending:
$ git status On branch almond_butter Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: almond_butter.txt
…and you’d like to rebase your branch onto
main. To do so with automatic stashing and unstashing, use the
$ git rebase -i main --autostash
-i is interactive mode. I always use it for rebasing so that I can see what will happen.)
When the rebase runs, Git will report about the stashing and unstashing:
$ git rebase -i main --autostash Created autostash: 684cb15 Applied autostash. Successfully rebased and updated refs/heads/almond_butter.
And just like that, it’s done! Three commands for the price of one.
It’s possible that the final step to unstash will result in merge conflicts, just like if you manually stashed changes. In this case, Git will not apply the stash, and instead report the situation:
$ git rebase -i main --autostash Created autostash: e7cf2ca Applying autostash resulted in conflicts. Your changes are safe in the stash. You can run "git stash pop" or "git stash drop" at any time. Successfully rebased and updated refs/heads/almond_butter.
Follow either of the commands in the message:
git stash popto apply the stash.
You’ll need to then fix the conflicts as per usual.
git stash dropto drop the stash.
This discards it from the stash stack. Useful if the changes are no longer relevant after the rebase, or you can easily redo them.
Enable autostash globally
rebase.autoStash in your global Git config to enable rebase autostashing by default:
$ git config --global rebase.autoStash true
This will add to your
[rebase] autoStash = true
No need to pass
If you want to run a rebase without autostashing, you can pass
$ git rebase -i main --no-autostash
Essentially, this option makes rebase refuse to run if there are pending changes:
$ git rebase -i main --no-autostash error: cannot rebase: You have unstaged changes. error: Please commit or stash them.
Not all that useful, in my opinion, but may find its place in an alias or script.
How to merge with autostash
git merge also has an
--autostash option, which acts similarly:
$ git merge --autostash main Created autostash: 97a4da3 Merge made by the 'ort' strategy. Applied autostash.
You can also enable this globally with
$ git config --global merge.autoStash true
…which adds to your
[merge] autoStash = true
--no-autostash to disable the behaviour for a particular
git merge invocation.
If your Django project’s long test runs bore you, I wrote a book that can help.
One summary email a week, no spam, I pinky promise.
- Git: How to enable autocorrect
- Git: How to automatically create upstream branches
- Git: How to undo commits