You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Worktree friction: copy staged and unstaged changes when creating new worktree #938
When I'm on the main branch and start working on changes, I sometimes realize mid-way that this should be a separate branch/worktree. However, if I have unstaged (or staged) changes and want to switch to a new worktree, there's no way to bring those changes along.
The changes are stuck in my current worktree, and I have to manually move them over before I can continue working in the new worktree.
Current workaround
Use git stash to temporarily store changes, switch to the new worktree, then pop them:
# In main worktree with uncommitted changes
git stash --include-untracked
wt switch --create feature-xyz
git stash pop
This is clunky and error-prone; it's easy to forget to pop, or to accidentally pop in the wrong worktree if you have multiple stashes.
Ideal solution
Two mutually exclusive flags for wt switch --create: --copy-staged: Copy only staged changes to the new worktree --copy-changes: Copy all changes (staged + unstaged) to the new worktree
would be keen to see whether others have similar issues. we should have a fairly high bar for new options; and my sense is that this might fall the wrong side
(fwiw I used to have this problem more often, but now basically never do anything on main. but also don't want to overfit on my own experience)
@max-sixty first of all, great tool! I was battling today with setting up (it may or may not have involved LLMs) my own workflow until I discovered this by accident and am happy to see a solid solution out there. Can't wait to properly try it out.
That being said, I also ran into the above issue. I will give your suggestion a try (which is not using main really) and if I still bump into same issues as @frederik-suerig I will reach out 🙏🏻
Here are two [aliases] entries that compose wt switch --create with git's stash/diff plumbing. I tested both end-to-end against a scratch repo with mixed staged + unstaged + untracked changes.
# .config/wt.toml or ~/.config/worktrunk/config.toml
[aliases]
# Take all in-progress changes (staged + unstaged + untracked) along to a new# worktree. Source becomes clean.# wt step move-changes --var to=feature-xyzmove-changes = '''if git diff --quiet HEAD && test -z "$(git ls-files --others --exclude-standard)"; then wt switch --create {{ to }}; else git stash push --include-untracked --quiet && wt switch --create {{ to }} --execute='git stash pop --index'; fi'''# Copy only staged changes to a new worktree. Source is unchanged.# wt step copy-staged --var to=feature-xyzcopy-staged = '''if git diff --cached --quiet; then wt switch --create {{ to }}; else p=$(mktemp) && git diff --cached > "$p" && wt switch --create {{ to }} --execute="git apply --index '$p' && rm '$p'"; fi'''
How they work:
move-changes stashes everything (--include-untracked), creates the new worktree, then uses --execute='git stash pop --index' so the pop runs in the new worktree. --index preserves the staged-vs-unstaged split. The git diff --quiet HEAD gate skips the stash dance when the source is already clean — avoiding spurious git stash pop noise and making sure we never accidentally pop a pre-existing stash.
copy-staged writes git diff --cached to a tempfile and applies it in the new worktree via git apply --index. The source worktree is left untouched (true copy semantics, matching the issue's --copy-staged description). Unlike git stash --staged, the diff approach handles files where staged and unstaged hunks overlap.
One important caveat: wt step aliases can't change the parent shell's directory — by design, wt step strips WORKTRUNK_DIRECTIVE_FILE from alias subprocesses, so the inner wt switch --create can't propagate a cd back through shell integration. After the alias, follow up with wt switch <branch> to land in the new worktree, or chain it on one line:
The trailing wt switch writes the cd directive normally. Inside the alias you'll also see a Cannot change directory — shell integration not installed warning from the inner wt switch — that's the same env-stripping at work, and it's harmless here.
Tested cases (scratch repo, git status --porcelain on each side after the alias):
Scenario
move-changes
copy-staged
Clean source
source/new both clean
source/new both clean
Staged + unstaged + untracked
source clean, new has MM + ??
source unchanged, new has M (staged only, no untracked)
Untracked-only
source clean, new has ??
n/a
Empty staged area
n/a
source/new both clean
If --base differs from HEAD, git apply / git stash pop may reject hunks that don't match — same constraint the issue calls out for a native flag.
Now that #2077 is in main, an inner wt switch --create inside an alias body propagates its cd to the parent shell (docs) — so the chained wt step <alias> && wt switch <branch> workaround is gone, and we can fold the third variant from the issue (--copy-changes) in too. Updated [aliases]:
# .config/wt.toml or ~/.config/worktrunk/config.toml
[aliases]
# Move all in-progress changes (staged + unstaged + untracked) to a new# worktree. Source becomes clean.# wt step move-changes --var to=feature-xyzmove-changes = '''if git diff --quiet HEAD && test -z "$(git ls-files --others --exclude-standard)"; then wt switch --create {{ to }}; else git stash push --include-untracked --quiet && wt switch --create {{ to }} --execute='git stash pop --index'; fi'''# Copy all changes (staged + unstaged + untracked) to a new worktree.# Source is unchanged.# wt step copy-changes --var to=feature-xyzcopy-changes = '''if git diff --quiet HEAD && test -z "$(git ls-files --others --exclude-standard)"; then wt switch --create {{ to }}; else git stash push --include-untracked --quiet && git stash apply --index --quiet && wt switch --create {{ to }} --execute='git stash pop --index'; fi'''# Copy only staged changes to a new worktree. Source is unchanged.# wt step copy-staged --var to=feature-xyzcopy-staged = '''if git diff --cached --quiet; then wt switch --create {{ to }}; else p=$(mktemp) && git diff --cached > "$p" && wt switch --create {{ to }} --execute="git apply --index '$p' && rm '$p'"; fi'''
Usage: wt step move-changes --var to=feature-xyz (similarly for the others). Add --yes to skip the project-config approval prompt on first run.
How they work:
move-changes stashes everything (--include-untracked), creates the new worktree, then --execute='git stash pop --index' runs the pop in the new worktree. --index preserves the staged-vs-unstaged split. The clean-state guard avoids touching a pre-existing stash.
copy-changes is the same dance plus an immediate git stash apply --index --quiet after the push, which restores the source worktree without dropping the stash. The new worktree pops the same stash via --execute. Net effect: identical states on both sides, untracked files included, no tempfile.
copy-staged writes git diff --cached to a tempfile and applies it in the new worktree via git apply --index. Diff (rather than git stash --staged) handles files where staged and unstaged hunks overlap on the same lines.
Tested end-to-end against a scratch repo (git status --porcelain on each side after the alias + sourcing the cd directive in the parent shell):
Scenario
move-changes
copy-changes
copy-staged
Clean source
source/new clean
source/new clean
source/new clean
Staged + unstaged + untracked
source clean, new MM + ??
source MM + ??, new MM + ??
source unchanged, new M only
Untracked-only
source clean, new ??
source ??, new ??
n/a
If --base differs from HEAD, git apply / git stash pop may reject hunks that don't match — same constraint the issue calls out for a native flag.
Activity
max-sixty commentedon Feb 5, 2026
would be keen to see whether others have similar issues. we should have a fairly high bar for new options; and my sense is that this might fall the wrong side
(fwiw I used to have this problem more often, but now basically never do anything on
main. but also don't want to overfit on my own experience)pkey commentedon Mar 11, 2026
@max-sixty first of all, great tool! I was battling today with setting up (it may or may not have involved LLMs) my own workflow until I discovered this by accident and am happy to see a solid solution out there. Can't wait to properly try it out.
That being said, I also ran into the above issue. I will give your suggestion a try (which is not using
mainreally) and if I still bump into same issues as @frederik-suerig I will reach out 🙏🏻ortonomy commentedon Mar 16, 2026
Would love this feature @max-sixty - already annoyed it doesn't do this, since this is the natural behaviour for
git checkout -b ...max-sixty commentedon Apr 11, 2026
@worktrunk-bot can you give an example of an alias that would do this?
worktrunk-bot commentedon Apr 11, 2026
max-sixty commentedon Apr 11, 2026
@worktrunk-bot please create worktrunk aliases
worktrunk-bot commentedon Apr 11, 2026
Here are two
[aliases]entries that composewt switch --createwith git's stash/diff plumbing. I tested both end-to-end against a scratch repo with mixed staged + unstaged + untracked changes.How they work:
move-changesstashes everything (--include-untracked), creates the new worktree, then uses--execute='git stash pop --index'so the pop runs in the new worktree.--indexpreserves the staged-vs-unstaged split. Thegit diff --quiet HEADgate skips the stash dance when the source is already clean — avoiding spuriousgit stash popnoise and making sure we never accidentally pop a pre-existing stash.copy-stagedwritesgit diff --cachedto a tempfile and applies it in the new worktree viagit apply --index. The source worktree is left untouched (true copy semantics, matching the issue's--copy-stageddescription). Unlikegit stash --staged, the diff approach handles files where staged and unstaged hunks overlap.One important caveat:
wt stepaliases can't change the parent shell's directory — by design,wt stepstripsWORKTRUNK_DIRECTIVE_FILEfrom alias subprocesses, so the innerwt switch --createcan't propagate acdback through shell integration. After the alias, follow up withwt switch <branch>to land in the new worktree, or chain it on one line:wt step move-changes --var to=feature-xyz --yes && wt switch feature-xyzThe trailing
wt switchwrites thecddirective normally. Inside the alias you'll also see aCannot change directory — shell integration not installedwarning from the innerwt switch— that's the same env-stripping at work, and it's harmless here.Tested cases (scratch repo,
git status --porcelainon each side after the alias):move-changescopy-stagedMM+??M(staged only, no untracked)??If
--basediffers fromHEAD,git apply/git stash popmay reject hunks that don't match — same constraint the issue calls out for a native flag.max-sixty commentedon Apr 11, 2026
open an issue re being able to change paths from an
alias, either explicitly or at least continuing let the cd pass through from an inner commandworktrunk-bot commentedon Apr 11, 2026
Opened #2075.
max-sixty commentedon Apr 11, 2026
ok, that merged; now please update the suggested worktrunk aliases to be better
worktrunk-bot commentedon Apr 11, 2026
Now that #2077 is in main, an inner
wt switch --createinside an alias body propagates itscdto the parent shell (docs) — so the chainedwt step <alias> && wt switch <branch>workaround is gone, and we can fold the third variant from the issue (--copy-changes) in too. Updated[aliases]:Usage:
wt step move-changes --var to=feature-xyz(similarly for the others). Add--yesto skip the project-config approval prompt on first run.How they work:
move-changesstashes everything (--include-untracked), creates the new worktree, then--execute='git stash pop --index'runs the pop in the new worktree.--indexpreserves the staged-vs-unstaged split. The clean-state guard avoids touching a pre-existing stash.copy-changesis the same dance plus an immediategit stash apply --index --quietafter the push, which restores the source worktree without dropping the stash. The new worktree pops the same stash via--execute. Net effect: identical states on both sides, untracked files included, no tempfile.copy-stagedwritesgit diff --cachedto a tempfile and applies it in the new worktree viagit apply --index. Diff (rather thangit stash --staged) handles files where staged and unstaged hunks overlap on the same lines.Tested end-to-end against a scratch repo (
git status --porcelainon each side after the alias + sourcing thecddirective in the parent shell):move-changescopy-changescopy-stagedMM+??MM+??, newMM+??Monly????, new??If
--basediffers fromHEAD,git apply/git stash popmay reject hunks that don't match — same constraint the issue calls out for a native flag.max-sixty commentedon Apr 11, 2026
OK great, add these examples to the new
Extending worktrunkpage and then close thisdocs(extending): add recipe for carrying changes to a new worktree
worktrunk-bot commentedon Apr 11, 2026
Opened #2083 with all three aliases added to the Extending Worktrunk → Aliases → Recipe section. Closing per request — reopen if the PR needs to go a different direction.
docs(extending): add aliases for moving in-progress changes
worktrunk-bot commentedon Apr 11, 2026
Opened #2084.
docs(extending): add recipe for carrying changes to a new worktree (#…