brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git rebase --autostash lets you rebase with a dirty working tree

Git
~/tips/tip-git-rebase-autostash

git rebase --autostash origin/main

Stashes uncommitted changes before a rebase and restores them afterwards.

A rebase refuses to start while you have uncommitted changes. The stash, rebase, pop routine works, but it is three commands and easy to forget the last one. --autostash does all three for you.

Git creates a temporary stash before the rebase and applies it when the rebase is done:

git rebase --autostash origin/main

Without the flag you get this instead:

error: cannot rebase: You have unstaged changes.
error: Please commit or stash them.

With the flag, Git prints Created autostash at the start and Applied autostash. at the end, and your edits are back in the working tree. If you want this every time, set it once. --no-autostash switches it off for a single run.

git config --global rebase.autoStash true

The docs warn that the final stash application after a successful rebase might result in non-trivial conflicts. That happens when your uncommitted edit touches a line the rebased commits changed. Git then says Applying autostash resulted in conflicts and tells you that your changes are safe in the stash. The rebase itself is finished at that point, the stash entry stays in git stash list, and you resolve the conflict by hand. Run git stash drop once you are happy with the result.

For more than a few small edits I would still commit a work-in-progress commit first. A commit is easier to get back than a stash entry you forgot about.

Further reading: git-rebase documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link