Remove deletes the worktree's directory and its branch, discarding any uncommitted changes, untracked files, and commits. Callers decide when removal is appropriate (e.g. only for worktrees they created, never pre-existing ones).
(ctx context.Context)
| 301 | // removal is appropriate (e.g. only for worktrees they created, never |
| 302 | // pre-existing ones). |
| 303 | func (wt *Worktree) Remove(ctx context.Context) error { |
| 304 | if err := git(ctx, wt.SourceDir, "worktree", "remove", "--force", wt.Dir); err != nil { |
| 305 | return fmt.Errorf("removing git worktree: %w", err) |
| 306 | } |
| 307 | // The branch can only be deleted once the worktree no longer occupies it; |
| 308 | // -D discards unmerged commits, which is the intended "remove and forget". |
| 309 | if err := git(ctx, wt.SourceDir, "branch", "-D", wt.Branch); err != nil { |
| 310 | return fmt.Errorf("deleting worktree branch: %w", err) |
| 311 | } |
| 312 | return nil |
| 313 | } |
| 314 | |
| 315 | // validateName rejects names that would escape the worktrees directory or |
| 316 | // produce an invalid git branch. Names must be a single path segment made of |