Skip to content

Worktree friction: add flag to automatically cd into previous subdirectory #937

Closed

Description

@frederik-suerig

The friction

When running wt switch --create {name} from a subdirectory within a worktree, worktrunk creates/switches to the new worktree correctly, but always cd into the worktree root directory instead of the equivalent subdirectory.

This is especially painful in monorepos — if I'm working in ./apps/gateway/ and need to create a feature branch, I end up at the repo root and have to navigate back to the service I was working on. In large monorepos with many services, this adds friction every time I switch branches.

Current workaround

After switching, manually cd back into the subdirectory:

# In ~/project.main/apps/gateway/
wt switch --create feature-XYZ
cd apps/gateway/

Ideal solution

A --subdir (-s) flag (and corresponding config option) that automatically cds into the same subdirectory in the new worktree, if it exists:

# In ~/project.main/apps/gateway/
wt switch --subdir --create feature-XYZ
# Now in ~/project.feature-button/apps/gateway/

If the subdirectory doesn't exist in the target worktree, fall back to the worktree root (silent fallback — the user will see they're at root).

Config option to enable globally:

[switch]
preserve-subdir = true

Activity

  1. max-sixty commented on Feb 5, 2026

    @max-sixty
    Owner

    agree!

    should we just always cd into the subdir?

  2. changed the title [-]Worktree friction: add feature flag to automatically cd into previous subdirectory[/-] [+]Worktree friction: add flag to automatically cd into previous subdirectory[/+] on Feb 5, 2026
  3. frederik-suerig commented on Feb 5, 2026

    @frederik-suerig
    Author

    There are cases where ending up at the worktree root makes sense too. For example: I'm deep in some subdirectory, create a new worktree to work on something completely different, and actually want to start fresh at the root rather than in the same nested location.

    That said, I think for most workflows, especially in monorepos, preserving the subdirectory is the more common need. When you're fixing a bug in apps/gateway/ and need to branch off, you probably want to stay in apps/gateway/.

    A few design options:

    1. Default to preserving subdir, flag to override: wt switch --create foo preserves subdir by default; add --root (or -r) to explicitly land at root. Config option to flip the default.
    2. Default to root (current behavior), flag to preserve: Add --subdir (or -s) as proposed. Config option to make it the default.
    3. Always preserve subdir, no flag: Simplest, but loses flexibility for the "fresh start at root" case.

    I'd lean toward option 1, preserving subdir feels like the less surprising behavior for most users, especially in monorepos where the friction is most acute. Users who want root can be explicit about it. But I could see an argument for option 2 if we want to be conservative about changing defaults.

  4. max-sixty commented on Feb 5, 2026

    @max-sixty
    Owner

    let's go with (3) for the moment; I think we should have quite a high bar on adding options, and this could compose with a cd (git rev-parse --git-common-dir)?

    if that proves insufficient we can consider adding something

  5. added a commit that references this issue on Feb 5, 2026
    3e4dfb6
  6. added a commit that references this issue on Feb 6, 2026
    114b5eb
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