@@ -39,6 +39,7 @@ func runRemove(cmd *cobra.Command, args []string) error {
3939 }
4040
4141 filtered := filterManagedWorktrees (worktrees , projectRoot )
42+ mainBranch := cfg .MainBranchOrDefault ()
4243
4344 if len (filtered ) == 0 {
4445 return fmt .Errorf ("no worktrees found" )
@@ -78,11 +79,6 @@ func runRemove(cmd *cobra.Command, args []string) error {
7879 }
7980 }
8081
81- // Protect the main branch from accidental removal.
82- if selected .Branch == cfg .MainBranchOrDefault () {
83- return fmt .Errorf ("cannot remove the main branch worktree (%s)" , selected .Branch )
84- }
85-
8682 force , _ := cmd .Flags ().GetBool ("force" )
8783
8884 if ! force {
@@ -108,13 +104,22 @@ func runRemove(cmd *cobra.Command, args []string) error {
108104 }
109105 }
110106
111- ui .Step ("Removing worktree: " + selected .Branch )
107+ isMainBranch := selected .Branch == mainBranch
108+
109+ if isMainBranch {
110+ ui .Step ("Removing worktree: " + selected .Branch + " (branch preserved in bare repo)" )
111+ } else {
112+ ui .Step ("Removing worktree: " + selected .Branch )
113+ }
114+
112115 if err := runner .WorktreeRemove (ctx , selected .Path , force ); err != nil {
113116 return err
114117 }
115118
116- if err := runner .BranchDelete (ctx , selected .Branch , false ); err != nil {
117- ui .Warning ("Could not delete branch: " + err .Error ())
119+ if ! isMainBranch {
120+ if err := runner .BranchDelete (ctx , selected .Branch , false ); err != nil {
121+ ui .Warning ("Could not delete branch: " + err .Error ())
122+ }
118123 }
119124
120125 ui .Success ("Removed worktree: " + selected .Branch )
0 commit comments