Skip to content

Commit 9b7d763

Browse files
committed
Add --remove / -r option to remove a specific worktree
Removes the worktree directory, safely deletes the local branch (-d), and cleans up empty parent directories.
1 parent 1fce9cd commit 9b7d763

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/git-wt/Commands.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,47 @@ public static int List()
206206
return 0;
207207
}
208208

209+
/// <summary>
210+
/// Removes a specific worktree and its local branch.
211+
/// </summary>
212+
public static int Remove(string branchName)
213+
{
214+
if (!TryFindBareRepo(out var bareRepoPath, out var repoRoot))
215+
return 1;
216+
217+
var (wtExit, wtOutput, _) = Git.Run(bareRepoPath, "worktree", "list", "--porcelain");
218+
if (wtExit != 0)
219+
{
220+
Console.Error.WriteLine("Error: Failed to list worktrees.");
221+
return 1;
222+
}
223+
224+
var worktrees = Parsing.ParseWorktreeList(wtOutput);
225+
var match = worktrees.FirstOrDefault(wt => !wt.IsBare && wt.Branch == branchName);
226+
if (match is null)
227+
{
228+
Console.Error.WriteLine($"Error: No worktree found for branch '{branchName}'.");
229+
return 1;
230+
}
231+
232+
Console.WriteLine($"Removing worktree '{branchName}'...");
233+
if (Git.RunLive(bareRepoPath, "worktree", "remove", match.Path) != 0)
234+
{
235+
Console.Error.WriteLine("Error: Failed to remove worktree. It may have dirty or untracked changes.");
236+
Console.Error.WriteLine("Use 'git worktree remove --force' to remove it anyway.");
237+
return 1;
238+
}
239+
240+
var (delExit, _, _) = Git.Run(bareRepoPath, "branch", "-d", branchName);
241+
if (delExit != 0)
242+
Console.Error.WriteLine($"Warning: Could not delete branch '{branchName}'. Remove manually with: git branch -D {branchName}");
243+
244+
RemoveEmptyParentDirectories(match.Path, repoRoot);
245+
246+
Console.WriteLine($"Removed worktree and branch '{branchName}'.");
247+
return 0;
248+
}
249+
209250
/// <summary>
210251
/// Removes worktrees whose upstream branch has been deleted (gone),
211252
/// then cleans up empty parent directories.

src/git-wt/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
Description = "Remove worktrees whose upstream branch is gone and delete empty parent directories"
1717
};
1818

19+
var removeOption = new Option<string>("--remove", "-r")
20+
{
21+
Description = "Remove a worktree and its local branch"
22+
};
23+
1924
var setupOption = new Option<string>("--setup", "-s")
2025
{
2126
Description = "Clone a repository into a bare worktree layout: <name>/.bare + default branch worktree"
@@ -26,6 +31,7 @@
2631
branchArg,
2732
listOption,
2833
pruneOption,
34+
removeOption,
2935
setupOption
3036
};
3137

@@ -41,6 +47,10 @@
4147
if (parseResult.GetValue(pruneOption))
4248
return Commands.Prune();
4349

50+
var removeBranch = parseResult.GetValue(removeOption);
51+
if (!string.IsNullOrEmpty(removeBranch))
52+
return Commands.Remove(removeBranch);
53+
4454
var branchName = parseResult.GetValue(branchArg);
4555
if (string.IsNullOrEmpty(branchName))
4656
{

0 commit comments

Comments
 (0)