Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(step relocate): allow relocating dirty linked worktrees (#3104)

Worktrunk Bot8c80e5a29c5a
diff --git a/docs/content/step.md b/docs/content/step.md
index 23abdd84a..87054132b 100644
--- a/docs/content/step.md
+++ b/docs/content/step.md
@@ -845,9 +845,15 @@ The main worktree can't be moved with `git worktree move`. Instead, relocate
switches it to the default branch and creates a new linked worktree at the
expected path. Untracked and gitignored files remain at the original location.
+### Dirty worktrees
+
+Linked worktrees relocate as-is — `git worktree move` carries uncommitted
+changes along. Only the main worktree skips when dirty (its `git checkout`
+refuses), unless `--commit` is passed.
+
### Skipped worktrees
-- **Dirty** (without `--commit`) — use `--commit` to auto-commit first
+- **Dirty main worktree** (without `--commit`) — use `--commit` to auto-commit first
- **Locked** — unlock with `git worktree unlock`
- **Target blocked** (without `--clobber`) — use `--clobber` to backup blocker
- **Detached HEAD** — no branch to compute expected path
diff --git a/skills/worktrunk/reference/step.md b/skills/worktrunk/reference/step.md
index 8a4e36fcf..700e2d50e 100644
--- a/skills/worktrunk/reference/step.md
+++ b/skills/worktrunk/reference/step.md
@@ -890,9 +890,15 @@ The main worktree can't be moved with `git worktree move`. Instead, relocate
switches it to the default branch and creates a new linked worktree at the
expected path. Untracked and gitignored files remain at the original location.
+### Dirty worktrees
+
+Linked worktrees relocate as-is — `git worktree move` carries uncommitted
+changes along. Only the main worktree skips when dirty (its `git checkout`
+refuses), unless `--commit` is passed.
+
### Skipped worktrees
-- **Dirty** (without `--commit`) — use `--commit` to auto-commit first
+- **Dirty main worktree** (without `--commit`) — use `--commit` to auto-commit first
- **Locked** — unlock with `git worktree unlock`
- **Target blocked** (without `--clobber`) — use `--clobber` to backup blocker
- **Detached HEAD** — no branch to compute expected path
diff --git a/src/cli/step.rs b/src/cli/step.rs
index 0039f9a4a..dcbcf25d8 100644
--- a/src/cli/step.rs
+++ b/src/cli/step.rs
@@ -664,9 +664,15 @@ The main worktree can't be moved with `git worktree move`. Instead, relocate
switches it to the default branch and creates a new linked worktree at the
expected path. Untracked and gitignored files remain at the original location.
+## Dirty worktrees
+
+Linked worktrees relocate as-is — `git worktree move` carries uncommitted
+changes along. Only the main worktree skips when dirty (its `git checkout`
+refuses), unless `--commit` is passed.
+
## Skipped worktrees
-- **Dirty** (without `--commit`) — use `--commit` to auto-commit first
+- **Dirty main worktree** (without `--commit`) — use `--commit` to auto-commit first
- **Locked** — unlock with `git worktree unlock`
- **Target blocked** (without `--clobber`) — use `--clobber` to backup blocker
- **Detached HEAD** — no branch to compute expected path
diff --git a/src/commands/relocate.rs b/src/commands/relocate.rs
index 459b33aff..7c3ea3d0a 100644
--- a/src/commands/relocate.rs
+++ b/src/commands/relocate.rs
@@ -260,9 +260,19 @@ pub fn validate_candidates(
continue;
}
- // Check dirty
+ let is_main = paths_match(&candidate.wt.path, repo_path);
+
+ // Check dirty.
+ //
+ // `git worktree move` carries modified-tracked and untracked files
+ // along with the worktree, so for linked worktrees we don't need to
+ // require a clean state. The main worktree is different: it can't be
+ // moved with `git worktree move`, and the fallback path runs
+ // `git checkout <default-branch>` which refuses to switch over
+ // uncommitted changes — so we still skip dirty main worktrees unless
+ // `--commit` was passed.
let worktree = repo.worktree_at(&candidate.wt.path);
- if worktree.is_dirty()? {
+ if worktree.is_dirty()? && (auto_commit || is_main) {
if auto_commit {
eprintln!(
"{}",
@@ -282,9 +292,12 @@ pub fn validate_candidates(
StageMode::None, // already staged above
)?;
} else {
+ // is_main without --commit
eprintln!(
"{}",
- warning_message(cformat!("Skipping <bold>{branch}</> (uncommitted changes)"))
+ warning_message(cformat!(
+ "Skipping <bold>{branch}</> (uncommitted changes in main worktree)"
+ ))
);
eprintln!(
"{}",
@@ -300,7 +313,6 @@ pub fn validate_candidates(
}
}
- let is_main = paths_match(&candidate.wt.path, repo_path);
validated.push(ValidatedCandidate {
wt: candidate.wt,
expected_path: candidate.expected_path,
diff --git a/tests/integration_tests/step_relocate.rs b/tests/integration_tests/step_relocate.rs
index b0e0f6ef5..7bf147020 100644
--- a/tests/integration_tests/step_relocate.rs
+++ b/tests/integration_tests/step_relocate.rs
@@ -186,7 +186,11 @@ fn test_relocate_target_exists(repo: TestRepo) {
);
}
-/// Test that dirty worktrees are skipped without --commit
+/// Test that dirty linked worktrees relocate cleanly without --commit.
+///
+/// `git worktree move` carries modified-tracked and untracked files along
+/// with the worktree, so there's no reason to require a clean state. Issue
+/// #3103.
#[rstest]
fn test_relocate_dirty_without_commit(repo: TestRepo) {
let parent = worktree_parent(&repo);
@@ -201,18 +205,84 @@ fn test_relocate_dirty_without_commit(repo: TestRepo) {
wrong_path.to_str().unwrap(),
]);
- // Make uncommitted changes
- fs::write(wrong_path.join("dirty.txt"), "uncommitted changes").unwrap();
+ // Make uncommitted changes - both modified and untracked
+ fs::write(wrong_path.join("dirty.txt"), "untracked file").unwrap();
+ // Modify a tracked file too (initial test commit creates README.md or similar).
+ let tracked = wrong_path.join("modified-tracked.txt");
+ fs::write(&tracked, "first").unwrap();
+ repo.git_command()
+ .args([
+ "-C",
+ wrong_path.to_str().unwrap(),
+ "add",
+ "modified-tracked.txt",
+ ])
+ .run()
+ .unwrap();
+ repo.git_command()
+ .args([
+ "-C",
+ wrong_path.to_str().unwrap(),
+ "commit",
+ "-m",
+ "add tracked",
+ ])
+ .run()
+ .unwrap();
+ fs::write(&tracked, "second").unwrap();
- // Relocate should skip dirty worktree
+ // Relocate should move the dirty worktree
assert_cmd_snapshot!(make_snapshot_cmd(&repo, "step", &["relocate"], None));
- // Verify the worktree was NOT moved
+ // Verify the worktree was moved to its expected location, carrying both
+ // the untracked and modified-tracked files with it.
+ let expected_path = parent.join("repo.feature");
assert!(
- wrong_path.exists(),
- "Dirty worktree should not be moved: {}",
+ expected_path.exists(),
+ "Dirty worktree should be moved: {}",
+ expected_path.display()
+ );
+ assert!(
+ !wrong_path.exists(),
+ "Old worktree path should no longer exist: {}",
wrong_path.display()
);
+ assert!(
+ expected_path.join("dirty.txt").exists(),
+ "Untracked file should travel with the worktree",
+ );
+ assert_eq!(
+ fs::read_to_string(expected_path.join("modified-tracked.txt")).unwrap(),
+ "second",
+ "Modified tracked file content should travel with the worktree",
+ );
+}
+
+/// Test that a dirty main worktree is still skipped — its relocation runs
+/// `git checkout <default-branch>` which refuses to switch over uncommitted
+/// changes.
+#[rstest]
+fn test_relocate_dirty_main_worktree_skipped(repo: TestRepo) {
+ let parent = worktree_parent(&repo);
+ let repo_path = repo.root_path().to_path_buf();
+
+ // Switch main worktree to a feature branch so it becomes a relocation
+ // candidate (expected path = repo.feature, not repo).
+ repo.run_git(&["checkout", "-b", "feature"]);
+
+ // Make uncommitted changes in main worktree
+ fs::write(repo_path.join("dirty.txt"), "uncommitted").unwrap();
+
+ // Relocate should skip the dirty main worktree
+ assert_cmd_snapshot!(make_snapshot_cmd(&repo, "step", &["relocate"], None));
+
+ // Main worktree stays put
+ let expected_path = parent.join("repo.feature");
+ assert!(
+ !expected_path.exists(),
+ "Dirty main worktree should not be relocated: {}",
+ expected_path.display()
+ );
}
/// Test that --commit auto-commits dirty worktrees before relocating
@@ -847,17 +917,12 @@ fn test_relocate_dry_run_json(repo: TestRepo) {
/// distinguishes relocated vs skipped (with stable `reason` codes).
#[rstest]
fn test_relocate_json_with_skip(repo: TestRepo) {
- let parent = worktree_parent(&repo);
- let wrong_path = parent.join("wrong-location");
- repo.run_git(&[
- "worktree",
- "add",
- "-b",
- "feature",
- wrong_path.to_str().unwrap(),
- ]);
- // Make the worktree dirty so it's skipped during validation
- fs::write(wrong_path.join("dirty.txt"), "uncommitted").unwrap();
+ let repo_path = repo.root_path().to_path_buf();
+ // Switch the main worktree to a feature branch so it becomes a relocation
+ // candidate, then dirty it. A dirty main worktree is skipped because its
+ // relocation runs `git checkout`, which won't switch over dirty state.
+ repo.run_git(&["checkout", "-b", "feature"]);
+ fs::write(repo_path.join("dirty.txt"), "uncommitted").unwrap();
let output = repo
.wt_command()
diff --git a/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_main_worktree_skipped.snap b/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_main_worktree_skipped.snap
new file mode 100644
index 000000000..1e5b00b8c
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_main_worktree_skipped.snap
@@ -0,0 +1,52 @@
+---
+source: tests/integration_tests/step_relocate.rs
+info:
+ program: wt
+ args:
+ - step
+ - relocate
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ LLVM_PROFILE_FILE: "[LLVM_PROFILE_FILE]"
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
+ PATH: "[PATH]"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_BASH_INSTALLED: "0"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_CODEX_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_FISH_INSTALLED: "0"
+ WORKTRUNK_TEST_GEMINI_INSTALLED: "0"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+▲ Skipping feature (uncommitted changes in main worktree)
+↳ To auto-commit changes before relocating, use --commit
+
+○ Skipped 1 worktree
diff --git a/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_without_commit.snap b/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_without_commit.snap
index d5b5027b4..3960632ab 100644
--- a/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_without_commit.snap
+++ b/tests/snapshots/integration__integration_tests__step_relocate__relocate_dirty_without_commit.snap
@@ -46,7 +46,6 @@ exit_code: 0
----- stdout -----
----- stderr -----
-▲ Skipping feature (uncommitted changes)
-↳ To auto-commit changes before relocating, use --commit
+✓ Relocated feature: _PARENT_/wrong-location → _REPO_.feature
-○ Skipped 1 worktree
+✓ Relocated 1 worktree