Skip to content

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

feat: Add --no-delete-branch option to `wt remove`

Maximilian Rooscffc49de492d
diff --git a/src/commands/worktree.rs b/src/commands/worktree.rs
index f76b90ed4..e15053148 100644
--- a/src/commands/worktree.rs
+++ b/src/commands/worktree.rs
@@ -143,6 +143,8 @@ pub enum RemoveResult {
primary_path: PathBuf,
worktree_path: PathBuf,
changed_directory: bool,
+ branch_name: String,
+ no_delete_branch: bool,
},
/// Switched to default branch in main repo
SwitchedToDefault(String),
@@ -271,7 +273,10 @@ pub fn handle_switch(
))
}
-pub fn handle_remove(worktree_name: Option<&str>) -> Result<RemoveResult, GitError> {
+pub fn handle_remove(
+ worktree_name: Option<&str>,
+ no_delete_branch: bool,
+) -> Result<RemoveResult, GitError> {
let repo = Repository::current();
// Resolve "@" to current branch early so progress message shows resolved name
@@ -292,13 +297,16 @@ pub fn handle_remove(worktree_name: Option<&str>) -> Result<RemoveResult, GitErr
// Two modes: remove current worktree vs. remove by name
match resolved_name.as_deref() {
- None => remove_current_worktree(&repo),
- Some(name) => remove_worktree_by_name(&repo, name),
+ None => remove_current_worktree(&repo, no_delete_branch),
+ Some(name) => remove_worktree_by_name(&repo, name, no_delete_branch),
}
}
/// Remove the current worktree (original behavior)
-fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError> {
+fn remove_current_worktree(
+ repo: &Repository,
+ no_delete_branch: bool,
+) -> Result<RemoveResult, GitError> {
// Check for uncommitted changes in current worktree
repo.ensure_clean_working_tree()?;
@@ -309,6 +317,9 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>
// In worktree: navigate to primary worktree and remove this one
// (no need to check default branch in this path)
let worktree_root = repo.worktree_root()?;
+ let current_branch = repo
+ .current_branch()?
+ .ok_or_else(|| GitError::CommandFailed("Not on a branch".to_string()))?;
let worktrees = repo.list_worktrees()?;
let primary_worktree_dir = worktrees.primary().path.clone();
@@ -317,6 +328,8 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>
primary_path: primary_worktree_dir,
worktree_path: worktree_root,
changed_directory: true, // We're in the worktree being removed
+ branch_name: current_branch,
+ no_delete_branch,
})
} else {
// In main repo: check if already on default branch
@@ -337,7 +350,11 @@ fn remove_current_worktree(repo: &Repository) -> Result<RemoveResult, GitError>
}
/// Remove a worktree by branch name (branch_name should already be resolved)
-fn remove_worktree_by_name(repo: &Repository, branch_name: &str) -> Result<RemoveResult, GitError> {
+fn remove_worktree_by_name(
+ repo: &Repository,
+ branch_name: &str,
+ no_delete_branch: bool,
+) -> Result<RemoveResult, GitError> {
// Find the worktree for this branch
let worktree_path = repo.worktree_for_branch(branch_name)?;
@@ -379,6 +396,8 @@ fn remove_worktree_by_name(repo: &Repository, branch_name: &str) -> Result<Remov
primary_path,
worktree_path,
changed_directory,
+ branch_name: branch_name.to_string(),
+ no_delete_branch,
})
}
diff --git a/src/main.rs b/src/main.rs
index 5a1dc51e3..ded1732be 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -317,21 +317,26 @@ Remove Multiple Worktrees:
CLEANUP:
-When removing a worktree:
+When removing a worktree (by default):
1. Validates worktree has no uncommitted changes
2. Changes directory (if removing current worktree)
3. Deletes worktree directory
4. Removes git worktree metadata
- 5. Deletes associated branch
+ 5. Deletes associated branch (uses git branch -d, safe delete)
+ - If branch has unmerged commits, shows warning but continues
+ - Use --no-delete-branch to skip branch deletion
EXAMPLES:
-Remove current worktree:
+Remove current worktree and branch:
wt remove
-Remove specific worktree:
+Remove specific worktree and branch:
wt remove feature-branch
+Remove worktree but keep branch:
+ wt remove --no-delete-branch feature-branch
+
Remove multiple worktrees:
wt remove old-feature another-branch
@@ -340,6 +345,10 @@ Switch to default in primary:
Remove {
/// Worktree names or branches to remove (use '@' for current, defaults to current if none specified)
worktrees: Vec<String>,
+
+ /// Don't delete the branch after removing the worktree (by default, branches are deleted)
+ #[arg(long = "no-delete-branch")]
+ no_delete_branch: bool,
},
/// Merge worktree into target branch
@@ -628,10 +637,14 @@ fn main() {
Ok(())
}),
- Commands::Remove { worktrees } => {
+ Commands::Remove {
+ worktrees,
+ no_delete_branch,
+ } => {
if worktrees.is_empty() {
// No worktrees specified, remove current worktree
- handle_remove(None).and_then(|result| handle_remove_output(&result, None))
+ handle_remove(None, no_delete_branch)
+ .and_then(|result| handle_remove_output(&result, None))
} else {
// When removing multiple worktrees, we need to handle the current worktree last
// to avoid deleting the directory we're currently in
@@ -663,13 +676,13 @@ fn main() {
// Remove others first, then current last
// Progress messages shown by handle_remove_output for all cases
for worktree in others.iter() {
- let result = handle_remove(Some(worktree.as_str()))?;
+ let result = handle_remove(Some(worktree.as_str()), no_delete_branch)?;
handle_remove_output(&result, Some(worktree.as_str()))?;
}
// Remove current worktree last (if it was in the list)
if let Some(current_name) = current {
- let result = handle_remove(Some(current_name.as_str()))?;
+ let result = handle_remove(Some(current_name.as_str()), no_delete_branch)?;
handle_remove_output(&result, Some(current_name.as_str()))?;
}
diff --git a/src/output/handlers.rs b/src/output/handlers.rs
index 224f84c6a..ae976dcad 100644
--- a/src/output/handlers.rs
+++ b/src/output/handlers.rs
@@ -36,18 +36,29 @@ fn format_remove_message(result: &RemoveResult, branch: Option<&str>) -> String
RemoveResult::RemovedWorktree {
primary_path,
changed_directory,
+ branch_name,
+ no_delete_branch,
..
} => {
+ // Build the action description
+ let action = if *no_delete_branch {
+ "Removed worktree"
+ } else {
+ "Removed worktree & branch"
+ };
+
let branch_suffix = branch
+ .or(Some(branch_name))
.map(|b| format!(" for {green_bold}{b}{green_bold:#}"))
.unwrap_or_default();
+
if *changed_directory {
format!(
- "{SUCCESS_EMOJI} {GREEN}Removed worktree{branch_suffix}, returned to primary at {green_bold}{}{green_bold:#}{GREEN:#}",
+ "{SUCCESS_EMOJI} {GREEN}{action}{branch_suffix}, returned to primary at {green_bold}{}{green_bold:#}{GREEN:#}",
primary_path.display()
)
} else {
- format!("{SUCCESS_EMOJI} {GREEN}Removed worktree{branch_suffix}{GREEN:#}")
+ format!("{SUCCESS_EMOJI} {GREEN}{action}{branch_suffix}{GREEN:#}")
}
}
RemoveResult::SwitchedToDefault(branch) => {
@@ -107,6 +118,8 @@ pub fn handle_remove_output(result: &RemoveResult, branch: Option<&str>) -> Resu
primary_path,
worktree_path,
changed_directory,
+ branch_name,
+ no_delete_branch,
} = result
{
// 1. Emit cd directive if needed - shell will execute this immediately
@@ -120,6 +133,28 @@ pub fn handle_remove_output(result: &RemoveResult, branch: Option<&str>) -> Resu
let repo = worktrunk::git::Repository::current();
repo.remove_worktree(worktree_path)
.git_context("Failed to remove worktree")?;
+
+ // 3. Delete the branch (unless --no-delete-branch was specified)
+ if !no_delete_branch {
+ // Create a Repository instance from the primary path to ensure we're running
+ // the command from a valid directory (the worktree we just removed may have
+ // been the current directory)
+ let primary_repo = worktrunk::git::Repository::at(primary_path);
+
+ // Use safe delete (-d) which fails if branch has unmerged commits
+ let result = primary_repo.run_command(&["branch", "-d", branch_name]);
+ if let Err(e) = result {
+ // If branch deletion fails, show a warning but don't error
+ // This matches the user's request: "print a nice message, don't raise some big error"
+ use worktrunk::styling::{WARNING, WARNING_EMOJI};
+ let warning_bold = WARNING.bold();
+ // Normalize error message to single line to prevent formatting issues
+ let error_msg = e.to_string().replace('\n', " ").trim().to_string();
+ super::progress(format!(
+ "{WARNING_EMOJI} {WARNING}Could not delete branch {warning_bold}{branch_name}{warning_bold:#}: {error_msg}{WARNING:#}"
+ ))?;
+ }
+ }
}
// Show success message (includes emoji and color)
diff --git a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap
index fa1d4e86a..4f81c4f6c 100644
--- a/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap
+++ b/tests/integration_tests/snapshots/integration__integration_tests__shell_wrapper__tests__remove.snap
@@ -1,6 +1,6 @@
---
source: tests/integration_tests/shell_wrapper.rs
-expression: output.normalized().as_ref()
+expression: output.normalized()
---
πŸ”„ Removing worktree for to-remove...
-βœ… Removed worktree for to-remove
+βœ… Removed worktree & branch for to-remove
diff --git a/tests/snapshots/integration__integration_tests__init__init_bash.snap b/tests/snapshots/integration__integration_tests__init__init_bash.snap
index e4e84eb42..65f9be467 100644
--- a/tests/snapshots/integration__integration_tests__init__init_bash.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_bash.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpIW4lMG/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpIA7lzg/test-config.toml
---
success: true
exit_code: 0
@@ -481,7 +481,7 @@ _wt() {
return 0
;;
wt__remove)
- opts="-v -h --verbose --help [WORKTREES]..."
+ opts="-v -h --no-delete-branch --verbose --help [WORKTREES]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
diff --git a/tests/snapshots/integration__integration_tests__init__init_fish.snap b/tests/snapshots/integration__integration_tests__init__init_fish.snap
index 5933fc856..692625e2d 100644
--- a/tests/snapshots/integration__integration_tests__init__init_fish.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_fish.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmprYnT86/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpwoMAjs/test-config.toml
---
success: true
exit_code: 0
@@ -280,6 +280,7 @@ complete -c wt -n "__fish_wt_using_subcommand switch" -s f -l force -d 'Skip con
complete -c wt -n "__fish_wt_using_subcommand switch" -l no-verify -d 'Skip all project hooks (post-create, post-start)'
complete -c wt -n "__fish_wt_using_subcommand switch" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand switch" -s h -l help -d 'Print help'
+complete -c wt -n "__fish_wt_using_subcommand remove" -l no-delete-branch -d 'Don\'t delete the branch after removing the worktree (by default, branches are deleted)'
complete -c wt -n "__fish_wt_using_subcommand remove" -s v -l verbose -d 'Enable verbose output (show git commands and debug info)'
complete -c wt -n "__fish_wt_using_subcommand remove" -s h -l help -d 'Print help'
complete -c wt -n "__fish_wt_using_subcommand merge" -l no-squash -d 'Disable squashing commits (by default, commits are squashed into one before merging)'
diff --git a/tests/snapshots/integration__integration_tests__init__init_zsh.snap b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
index cf3d1d834..bccae9aa1 100644
--- a/tests/snapshots/integration__integration_tests__init__init_zsh.snap
+++ b/tests/snapshots/integration__integration_tests__init__init_zsh.snap
@@ -16,7 +16,7 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpacy9En/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpO74VSQ/test-config.toml
---
success: true
exit_code: 0
@@ -329,6 +329,7 @@ _arguments "${_arguments_options[@]}" : \
;;
(remove)
_arguments "${_arguments_options[@]}" : \
+'--no-delete-branch[Don'\''t delete the branch after removing the worktree (by default, branches are deleted)]' \
'-v[Enable verbose output (show git commands and debug info)]' \
'--verbose[Enable verbose output (show git commands and debug info)]' \
'-h[Print help]' \
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap
index 696854565..51b066d46 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_main.snap
@@ -16,12 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpYRN3ow/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpjpw5Wg/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-a...
-βœ… Removed worktree for feature-a
+βœ… Removed worktree & branch for feature-a
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap
index 2abaecb61..31cccb2b3 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_from_other_worktree.snap
@@ -16,12 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpkl7qTG/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp8wSstb/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-b...
-βœ… Removed worktree for feature-b
+βœ… Removed worktree & branch for feature-b
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap b/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap
index 916eb609e..9a8a7249e 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_current_by_name.snap
@@ -16,12 +16,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpEsMvsL/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp5limTL/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-current...
-βœ… Removed worktree for feature-current, returned to primary at [REPO]
+βœ… Removed worktree & branch for feature-current, returned to primary at [REPO]
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap
index 39a9348d5..ec897a4d2 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_from_worktree.snap
@@ -15,12 +15,12 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpZPWxJo/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpjheYz9/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree...
-βœ… Removed worktree, returned to primary at [REPO]
+βœ… Removed worktree & branch for feature-wt, returned to primary at [REPO]
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap
index 01780bdca..3c66de82b 100644
Binary files a/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap and b/tests/snapshots/integration__integration_tests__remove__remove_internal_mode.snap differ
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap
index 49625f3a3..2b593fd6a 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_including_current.snap
@@ -18,16 +18,16 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmplyBFa9/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp4D6efo/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-b...
-βœ… Removed worktree for feature-b
+βœ… Removed worktree & branch for feature-b
πŸ”„ Removing worktree for feature-c...
-βœ… Removed worktree for feature-c
+βœ… Removed worktree & branch for feature-c
πŸ”„ Removing worktree for feature-a...
-βœ… Removed worktree for feature-a, returned to primary at [REPO]
+βœ… Removed worktree & branch for feature-a, returned to primary at [REPO]
----- stderr -----
diff --git a/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap b/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap
index fd7c313fd..8a3b6060a 100644
--- a/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap
+++ b/tests/snapshots/integration__integration_tests__remove__remove_multiple_worktrees.snap
@@ -18,16 +18,16 @@ info:
LANG: C
LC_ALL: C
SOURCE_DATE_EPOCH: "1761609600"
- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpu7MewE/test-config.toml
+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpXGv0t9/test-config.toml
---
success: true
exit_code: 0
----- stdout -----
πŸ”„ Removing worktree for feature-a...
-βœ… Removed worktree for feature-a
+βœ… Removed worktree & branch for feature-a
πŸ”„ Removing worktree for feature-b...
-βœ… Removed worktree for feature-b
+βœ… Removed worktree & branch for feature-b
πŸ”„ Removing worktree for feature-c...
-βœ… Removed worktree for feature-c
+βœ… Removed worktree & branch for feature-c
----- stderr -----