Skip to content

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

fix(remove): clear a branch's marker when wt deletes the branch (#4684)

Worktrunk Bot5bdb832fe835
diff --git a/docs/src/content/docs/config.md b/docs/src/content/docs/config.md
index 175f2762e..33cdb2feb 100644
--- a/docs/src/content/docs/config.md
+++ b/docs/src/content/docs/config.md
@@ -1464,6 +1464,8 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
+When `wt` deletes a branch, it clears that branch's marker.
+
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`.
### Command reference
diff --git a/docs/src/content/docs/faq.md b/docs/src/content/docs/faq.md
index c75d4f167..fb5d9769e 100644
--- a/docs/src/content/docs/faq.md
+++ b/docs/src/content/docs/faq.md
@@ -210,6 +210,8 @@ Use `-D` to force-delete branches with unmerged changes. Use `--no-delete-branch
A branch checked out in a second worktree is retained regardless, `-D` included. Deleting it would leave that worktree unable to resolve `HEAD`; only `git worktree add --force` produces that state.
+Deleting a branch also clears its [marker](/config/#wt-config-state-marker), so a later branch with the same name starts without one.
+
### Other cleanup
- `wt merge` / `wt step push` β€” the target branch's checked-out worktree is updated to the merged commits, so a file those commits delete disappears from it, and an ignored file at a path they track is overwritten β€” the same result a `git merge` run in that worktree would produce. Uncommitted changes at paths the merge doesn't touch stay in place, staged or not; one at a path it does touch refuses the merge upfront, naming the file
diff --git a/plugins/worktrunk/skills/worktrunk/reference/config.md b/plugins/worktrunk/skills/worktrunk/reference/config.md
index e813d7a37..5dfa6a0bb 100644
--- a/plugins/worktrunk/skills/worktrunk/reference/config.md
+++ b/plugins/worktrunk/skills/worktrunk/reference/config.md
@@ -1456,6 +1456,8 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
+When `wt` deletes a branch, it clears that branch's marker.
+
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`.
### Command reference
diff --git a/plugins/worktrunk/skills/worktrunk/reference/faq.md b/plugins/worktrunk/skills/worktrunk/reference/faq.md
index 2ff5e248d..898c49150 100644
--- a/plugins/worktrunk/skills/worktrunk/reference/faq.md
+++ b/plugins/worktrunk/skills/worktrunk/reference/faq.md
@@ -206,6 +206,8 @@ Use `-D` to force-delete branches with unmerged changes. Use `--no-delete-branch
A branch checked out in a second worktree is retained regardless, `-D` included. Deleting it would leave that worktree unable to resolve `HEAD`; only `git worktree add --force` produces that state.
+Deleting a branch also clears its [marker](https://worktrunk.dev/config/#wt-config-state-marker), so a later branch with the same name starts without one.
+
### Other cleanup
- `wt merge` / `wt step push` β€” the target branch's checked-out worktree is updated to the merged commits, so a file those commits delete disappears from it, and an ignored file at a path they track is overwritten β€” the same result a `git merge` run in that worktree would produce. Uncommitted changes at paths the merge doesn't touch stay in place, staged or not; one at a path it does touch refuses the merge upfront, naming the file
diff --git a/skills/worktrunk/reference/config.md b/skills/worktrunk/reference/config.md
index e813d7a37..5dfa6a0bb 100644
--- a/skills/worktrunk/reference/config.md
+++ b/skills/worktrunk/reference/config.md
@@ -1456,6 +1456,8 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
+When `wt` deletes a branch, it clears that branch's marker.
+
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`.
### Command reference
diff --git a/skills/worktrunk/reference/faq.md b/skills/worktrunk/reference/faq.md
index 2ff5e248d..898c49150 100644
--- a/skills/worktrunk/reference/faq.md
+++ b/skills/worktrunk/reference/faq.md
@@ -206,6 +206,8 @@ Use `-D` to force-delete branches with unmerged changes. Use `--no-delete-branch
A branch checked out in a second worktree is retained regardless, `-D` included. Deleting it would leave that worktree unable to resolve `HEAD`; only `git worktree add --force` produces that state.
+Deleting a branch also clears its [marker](https://worktrunk.dev/config/#wt-config-state-marker), so a later branch with the same name starts without one.
+
### Other cleanup
- `wt merge` / `wt step push` β€” the target branch's checked-out worktree is updated to the merged commits, so a file those commits delete disappears from it, and an ignored file at a path they track is overwritten β€” the same result a `git merge` run in that worktree would produce. Uncommitted changes at paths the merge doesn't touch stay in place, staged or not; one at a path it does touch refuses the merge upfront, naming the file
diff --git a/src/cli/config.rs b/src/cli/config.rs
index 64caf9ac5..64117a89d 100644
--- a/src/cli/config.rs
+++ b/src/cli/config.rs
@@ -1242,6 +1242,8 @@ Stored in git config as `worktrunk.state.<branch>.marker`. Set directly with:
$ git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
```
+When `wt` deletes a branch, it clears that branch's marker.
+
Without a subcommand, runs `get` for the current branch. For `--branch`, use `get --branch=NAME`."#
)]
Marker {
diff --git a/src/git/remove.rs b/src/git/remove.rs
index 0d028a5d7..bcf3066d8 100644
--- a/src/git/remove.rs
+++ b/src/git/remove.rs
@@ -645,6 +645,7 @@ pub fn delete_branch_if_safe(
// behavior for the Worktree path). The user explicitly chose -D.
if force_delete {
repo.run_command(&["branch", "-D", "--", branch_name])?;
+ clear_deleted_branch_marker(repo, branch_name);
return Ok(BranchDeletionResult {
outcome: BranchDeletionOutcome::ForceDeleted,
integration_target: target.to_string(),
@@ -673,6 +674,9 @@ pub fn delete_branch_if_safe(
}
None => BranchDeletionOutcome::NotDeleted,
};
+ if matches!(outcome, BranchDeletionOutcome::Integrated(_)) {
+ clear_deleted_branch_marker(repo, branch_name);
+ }
Ok(BranchDeletionResult {
outcome,
@@ -680,6 +684,22 @@ pub fn delete_branch_if_safe(
})
}
+/// Drop the activity marker of a branch `wt` just deleted.
+///
+/// Markers are keyed by branch name in shared git config, so one left behind
+/// would reappear on a later branch reusing the name. Best effort: the branch
+/// is already gone, so a failed unset is logged rather than surfaced.
+fn clear_deleted_branch_marker(repo: &Repository, branch_name: &str) {
+ // `<name>.vars.marker` is also branch `<name>`'s var `marker`; only
+ // marker JSON belongs to the deleted branch (as in `clear_all_markers`).
+ if repo.branch_marker(branch_name).is_none() {
+ return;
+ }
+ if let Err(e) = repo.unset_config(&format!("worktrunk.state.{branch_name}.marker")) {
+ tracing::debug!(error = %e, "Failed to clear marker for deleted branch {branch_name}: {e}");
+ }
+}
+
/// Find a live checkout of `branch` using a new repository cache.
///
/// Removal planning intentionally caches `git worktree list`, but branch
@@ -1158,6 +1178,88 @@ mod tests {
assert!(!exit.success(), "branch should have been deleted");
}
+ /// Deleting a branch drops its marker, so a later branch reusing the name
+ /// doesn't inherit it; a retained branch keeps its marker.
+ #[test]
+ fn deletion_clears_branch_marker() {
+ let test = TestRepo::with_initial_commit();
+ let repo = Repository::at(test.root_path()).unwrap();
+ for branch in ["integrated", "forced", "unmerged"] {
+ test.run_git(&["branch", branch]);
+ repo.set_config(
+ &format!("worktrunk.state.{branch}.marker"),
+ r#"{"marker":"πŸ€–","set_at":0}"#,
+ )
+ .unwrap();
+ }
+ test.run_git(&["checkout", "unmerged"]);
+ std::fs::write(test.root_path().join("work.txt"), "work\n").unwrap();
+ test.run_git(&["add", "work.txt"]);
+ test.run_git(&["commit", "-m", "unmerged work"]);
+ test.run_git(&["checkout", "main"]);
+
+ let snapshot = repo.capture_refs().unwrap();
+ for (branch, force) in [("integrated", false), ("forced", true), ("unmerged", false)] {
+ delete_branch_if_safe(&repo, &snapshot, branch, "main", force).unwrap();
+ }
+
+ let fresh = Repository::at(test.root_path()).unwrap();
+ assert_eq!(fresh.branch_marker("integrated"), None);
+ assert_eq!(fresh.branch_marker("forced"), None);
+ assert_eq!(fresh.branch_marker("unmerged").as_deref(), Some("πŸ€–"));
+ }
+
+ /// Deleting `<name>.vars` leaves branch `<name>`'s var named `marker`,
+ /// which shares the key `worktrunk.state.<name>.vars.marker`.
+ #[test]
+ fn deletion_keeps_var_sharing_marker_key() {
+ let test = TestRepo::with_initial_commit();
+ let repo = Repository::at(test.root_path()).unwrap();
+ test.run_git(&["branch", "feature.vars"]);
+ repo.set_config("worktrunk.state.feature.vars.marker", "plain value")
+ .unwrap();
+
+ let snapshot = repo.capture_refs().unwrap();
+ delete_branch_if_safe(&repo, &snapshot, "feature.vars", "main", true).unwrap();
+
+ let fresh = Repository::at(test.root_path()).unwrap();
+ assert_eq!(
+ fresh
+ .config_last("worktrunk.state.feature.vars.marker")
+ .unwrap()
+ .as_deref(),
+ Some("plain value")
+ );
+ }
+
+ /// A marker that can't be unset doesn't fail a deletion that already
+ /// happened; Git itself only warns when the locked config blocks its own
+ /// branch-section cleanup.
+ #[test]
+ fn deletion_survives_marker_unset_failure() {
+ let test = TestRepo::with_initial_commit();
+ let repo = Repository::at(test.root_path()).unwrap();
+ test.run_git(&["branch", "doomed"]);
+ repo.set_config(
+ "worktrunk.state.doomed.marker",
+ r#"{"marker":"πŸ€–","set_at":0}"#,
+ )
+ .unwrap();
+ let lock = test.root_path().join(".git/config.lock");
+ std::fs::write(&lock, "").unwrap();
+
+ let snapshot = repo.capture_refs().unwrap();
+ let result = delete_branch_if_safe(&repo, &snapshot, "doomed", "main", true).unwrap();
+ std::fs::remove_file(&lock).unwrap();
+
+ assert!(matches!(
+ result.outcome,
+ BranchDeletionOutcome::ForceDeleted
+ ));
+ let fresh = Repository::at(test.root_path()).unwrap();
+ assert_eq!(fresh.branch_marker("doomed").as_deref(), Some("πŸ€–"));
+ }
+
/// A packed-ref lock failure must preserve the branch and the original
/// Git error; an unchanged ref is not evidence of branch movement.
#[test]
diff --git a/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap b/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap
index 3d50a588e..d8680a4bb 100644
--- a/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap
+++ b/tests/snapshots/integration__integration_tests__help__help_config_state_marker.snap
@@ -97,6 +97,8 @@ Stored in git config as worktrunk.state.<branch>.marker. Set directly wi
  git config worktrunk.state.feature.marker '{"marker":"🚧","set_at":0}'
+When wt deletes a branch, it clears that branch's marker.
+
Without a subcommand, runs get for the current branch. For --branch, use get --branch=NAME.
----- stderr -----