SyntaxStudy
Sign Up
Git Advanced Stash: Branch from Stash
Git Beginner 7 min read

Advanced Stash: Branch from Stash

If applying a stash causes conflicts (because the branch has moved on), you can create a new branch directly from the stash using git stash branch. It checks out the commit the stash was created on, applies the stash, and drops it if successful.

Example
# Create a branch from a stash to avoid conflicts:
git stash branch feature/user-profile stash@{0}
# Equivalent to:
# 1. git checkout -b feature/user-profile <stash-commit>
# 2. git stash apply stash@{0}
# 3. git stash drop stash@{0}  (if apply succeeded)

# Inspect the contents of a stash before applying:
git stash show           # summary (files changed)
git stash show -p        # full diff
git stash show stash@{1} -p

# Stash only the staged changes (leave unstaged files alone):
git stash --staged
# Useful when you have two different kinds of changes
# and want to commit one set now, stash the other.

# Tip: regularly check for forgotten stashes:
git stash list

# A stash is stored as a commit under refs/stash.
# It is local — stashes are NOT pushed to the remote.