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 stash branch turns a stash into its own branch

Git
~/tips/tip-git-stash-branch-new-branch

git stash branch login-form stash@{0}

Creates a branch at the commit where you stashed and applies the stash there.

An old stash often refuses to apply because the branch moved on in the meantime. git stash branch sidesteps that by going back to where the stash was made.

git stash branch login-form stash@{0}

Git creates a new branch called login-form at the commit you were on when you made the stash, switches to it, and applies the stash there. Since the code is exactly as it was back then, it applies without conflicts. If that worked, the stash is dropped. Leave out the stash name and git uses the newest one.

From there it’s a normal branch. Commit the work, then rebase it on main or merge main into it, and solve conflicts the usual way, with commits to fall back on.

It also helps when a stash grows into something bigger than a quick save. Work that lives on a branch shows up in git branch, can be pushed and doesn’t get forgotten the way a stash does.

It even restores what was staged and what wasn’t, the way the stash recorded it, which I think is a nice touch.

Your working tree needs to be clean enough to switch branches. If you have other uncommitted changes that would be overwritten, git refuses and tells you which files are in the way. And the branch name must be new; git won’t reuse an existing one.

Further reading: git-stash 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