Synopsis
git stash push -p -m <msg> -- <pathspec>
git stash branch <newbranch> [<stash>]
git stash create
git stash store -m <msg> <sha>
Description
Beyond git stash push and git stash pop, the stash machinery offers powerful capabilities for advanced workflows. You can stash specific paths, stash specific hunks with -p, recreate a branch from a stash that conflicts with the current state, or even create a stash object without storing it on the stack (then store it later with a custom message).
Internally, each stash is a merge commit between WIP-on-branch (the working tree) and index-on-branch (the staged changes), with the parent being the branch tip. This structure is what allows stash branch and stash apply to reconstruct exact state.
In day-to-day use, git stash (advanced) integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git stash (advanced) --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git stash (advanced) interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git stash (advanced)'s side effects helps avoid all three.
Common Options
| Subcommand / Option | Description |
|---|---|
push -p | Stash interactively chosen hunks. |
push -- <pathspec> | Stash only specified paths. |
--keep-index | Stash but keep what's already staged. |
--no-keep-index | Reset index when stashing (default). |
branch <name> | Recreate state on a fresh branch. |
create | Make a stash commit but don't store it. |
store -m | Place an existing commit on the stack. |
Examples
git stash push -p -m "WIP: only refactor changes" -- src/refactor/
# Stash only specific paths, interactively
git stash --keep-index -m "Test pre-staged"
# Verify that staged changes pass tests, with WIP set aside
git stash branch fix-from-stash stash@{2}
# Resurrect a stash that no longer applies cleanly
SHA=$(git stash create "checkpoint")
git stash store -m "manual checkpoint" "$SHA"
# Programmatic stash creation
Common Mistakes
Stashing during a merge or rebase mixes states and confuses the stack. Avoid. Long-running stashes outlive their context — review periodically. --keep-index is great for verifying staged changes, but the resulting stash includes both staged and unstaged content, which can surprise.
Related Commands
git stash, git commit --fixup, git worktree, git restore