Skip to content

Worktree friction: copy staged and unstaged changes when creating new worktree #938

Closed

Description

@frederik-suerig

The friction

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

# Copy only what's staged
wt switch --create feature-xyz --copy-staged

# Copy everything (staged + unstaged)
wt switch --create feature-xyz --copy-changes

Important constraints:

  • These flags only make sense with --create (new worktree from a base branch)
    --copy-staged and --copy-changes should be mutually exclusive
  • Both should conflict with --base if the base branch differs from the current branch, as the changes might not apply cleanly
  • After copying, the original worktree should remain unchanged (changes are copied, not moved)

Activity

  1. max-sixty commented on Feb 5, 2026

    @max-sixty
    Owner

    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)

  2. pkey commented on Mar 11, 2026

    @pkey

    @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 🙏🏻

  3. ortonomy commented on Mar 16, 2026

    @ortonomy

    Would love this feature @max-sixty - already annoyed it doesn't do this, since this is the natural behaviour for git checkout -b ...

  4. max-sixty commented on Apr 11, 2026

    @max-sixty
    Owner

    @worktrunk-bot can you give an example of an alias that would do this?

  5. worktrunk-bot commented on Apr 11, 2026

    @worktrunk-bot
  6. max-sixty commented on Apr 11, 2026

    @max-sixty
    Owner

    @worktrunk-bot please create worktrunk aliases

  7. worktrunk-bot commented on Apr 11, 2026

    @worktrunk-bot
    Collaborator

    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-xyz
    move-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-xyz
    copy-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:

    wt step move-changes --var to=feature-xyz --yes && wt switch feature-xyz

    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.

  8. max-sixty commented on Apr 11, 2026

    @max-sixty
    Owner

    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 command

  9. worktrunk-bot commented on Apr 11, 2026

    @worktrunk-bot
    Collaborator

    Opened #2075.

  10. max-sixty commented on Apr 11, 2026

    @max-sixty
    Owner

    ok, that merged; now please update the suggested worktrunk aliases to be better

  11. worktrunk-bot commented on Apr 11, 2026

    @worktrunk-bot
    Collaborator

    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-xyz
    move-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-xyz
    copy-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-xyz
    copy-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.

  12. max-sixty commented on Apr 11, 2026

    @max-sixty
    Owner

    OK great, add these examples to the new Extending worktrunk page and then close this

  13. added a commit that references this issue on Apr 11, 2026
    af487ec
  14. worktrunk-bot commented on Apr 11, 2026

    @worktrunk-bot
    Collaborator

    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.

  15. worktrunk-bot commented on Apr 11, 2026

    @worktrunk-bot
    Collaborator

    Opened #2084.

  16. added a commit that references this issue on Apr 11, 2026
    61b0e5a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions