Git: How to automatically stash while rebasing or merging

A veritable stash of almonds (generated by Stable Diffusion)

Imagine you’re part way through making some changes, and you realize that you need to rebase or merge. You can:

  1. Save your work with git stash.
  2. Rebase or merge.
  3. 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:

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.

Fin

Now, where did I leave those almonds…

—Adam


If your Django project’s long test runs bore you, I wrote a book that can help.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: