The letter A styled as Alchemists logo. lchemists
Published August 14, 2024 Updated August 14, 2024
Cover
Git Rebase AutoStash

Git Rebase is a powerful workflow. Even better, you can configure Git to automatically stash what you are working on before rebasing and then pop the stash after the rebase has finished. In this article, we’ll explore how AutoStash works and ensure your Git configuration is updated to always automatically manage your stash when rebasing.

Table of Contents

Quick Start

To quickly configure Git with AutoStash enabled, run the following in your terminal:

git config --global rebase.autoStash true

That’s it! To learn more about this simple change, read on.

Tutorial

To start, let’s create a tutoral repository with a few commits so we can learn why AutoStash is valuable:

cd tutorial
git init
git config --global rebase.autoStash false

Now we can add our implmentation and documentation:

printf "%s\n" 'ARGV.then { |a, b| puts "#{a} + #{b} = #{a + b}" }' > add.rb
git add add.rb
git commit --all --message "Added addition script"

printf "%s\n" "Usage: ruby add.rb <number> <number>" > README.md
git add README.md
git commit --all --message "Added implementation documentation"

Opps, we made a mistake with our implementation so we can correct with a fixup:

printf "%s\n" 'ARGV.then { |a, b| puts "#{a} + #{b} = #{a.to_i + b.to_i}" }' > add.rb
git add add.rb
git commit --fixup $(git log --pretty=format:%h -1 HEAD^1)

We’ll rebase the fixup shortly but, first, let’s add a multiplication implementation:

printf "%s\n" 'ARGV.then { |a, b| puts "#{a} * #{b} = #{a.to_i * b.to_i}" }' > multiply.rb
git add multiply.rb

At this point, your Git history should look like this:

git log --oneline

# fixup! Added addition script
# Added implementation documentation
# Added addition script

It’d be nice to rebase and resolve the fixup! commit. Since this project hasn’t been shared yet, we’ll rebase from the repository root (i.e. the first commit made):

git rebase --interactive --root
# error: cannot rebase: Your index contains uncommitted changes.
# error: Please commit or stash them.

Notice the last error: "Please commit or stash them." Git wants us to stash changes before preceding because we still have a multiply.rb file we’ve not commited yet. To comply, we’ll manually stash and rebase:

git stash push --message "Work Prior to Rebase"
git rebase --interactive --root

The above works but don’t save these changes in your rebase editor. Instead, delete all lines, save, and exit to abort the rebase. With the rebase aborted, we can pretend the rebase finished and we want to continue working on our implmemention. The only problem is that we need to remember to pop our stash in order to get what we were last working on back:

git stash pop
git status --short --branch
# A  multiply.rb

Notice, multiply.rb is restored from the stash but we had to perform a couple manual steps to get back to a working state. Thankfully, Git can automate this for us via AutoStash which we can enable as follows:

git config --add rebase.autoStash true

With AutoStash enabled, let’s rebase again:

git rebase --interactive --root

This time, when we perform and complete the rebase, we get two important messages logged in our console:

# "Created autostash"
# "Applied autostash."

The above is Git letting us know a temporary stash was created, our work was moved into the stash, and our work was restored after the rebase (automatic popping of the stash). This can be verified by checking Git status:

git status --short --branch
# A  multiply.rb

We can also see the rebase cleaned up our history:

git log --oneline
# Added implementation documentation
# Added addition script

If you don’t have AutoStash enabled as part of your workflow, I recommend adding it. In fact, this setting is so handy, configuring it globally yields maximum benefit:

git config --global --add rebase.autoStash true

Conclusion

Using AutoStash — especially when set globally — will save you time by letting Git handle the manual labor that can get tedious if not automated. Enjoy!