Git: automatically stash while rebasing or merging with autostash

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
git stash. - Rebase or merge.
- Unstash your current changes with
git stash pop.
Since this is a useful sequence, Git has “autostash” options that make rebase and 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 almond_butter branch:
$ 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 --autostash option:
$ 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.
Hitting conflicts
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.
Alrighty then.
Enable autostash globally
Enable rebase.autoStash in your global Git config to enable rebase autostashing by default:
$ git config --global rebase.autoStash true
This will add to your ~/.gitconfig:
[rebase]
autoStash = true
No need to pass --autostash. Slick.
If you want to run a rebase without autostashing, you can pass --no-autostash:
$ 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 merge.autoStash:
$ git config --global merge.autoStash true
…which adds to your ~/.gitconfig:
[merge]
autoStash = true
Cool beans.
Equally, use --no-autostash to disable the behaviour for a particular git merge invocation.
😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸
One summary email a week, no spam, I pinky promise.
Related posts:
- Git: How to enable autocorrect
- Git: How to automatically create upstream branches
- Git: How to undo commits
Tags: git