Add "Detach worktree" option when checking out a branch occupied by another worktree#5341
Add "Detach worktree" option when checking out a branch occupied by another worktree#5341ruudk wants to merge 1 commit intojesseduffield:masterfrom
Conversation
|
Makes some sense, but I'm curious why you want this, rather than just switch to the worktree that has the branch already checked out. One reason that I could imagine: when I watch people use git (or lazygit), and they want to create a new branch from main, I often see them check out main first, pull it, and then create a new branch from it. So if main is checked out in a worktree, this would explain why you want your new feature. Note that this is unnecessary though: to create a new branch from main, simply select it in the branches list and press Any other reasons? |
|
I have my application and services booted in my main repository. The worktrees don't have that because it would eat resources. Occasionally I want to bring some of these features to my main repository and run it functionally. Does that make sense? |
|
Yes, thanks. I'm out of time today, so this PR probably won't make it for the release tomorrow; hope that's ok. |
|
Sure, no rush! Thanks for taking the time to review it |
…nother worktree When working with worktrees it's common to have a main project installation (e.g. running your database and services) alongside several worktrees for feature work. Previously, checking out a branch occupied by another worktree only offered "Switch to worktree", forcing you to switch to that worktree, detach it, then navigate back and check out the branch — a tedious round-trip. Now the prompt shows a menu with two options: "Switch to worktree" and "Detach worktree". The detach option runs `git checkout --detach` on the other worktree and then checks out the branch in one step. The detach option is disabled when the other worktree has uncommitted changes, to prevent accidental loss of work.
stefanhaller
left a comment
There was a problem hiding this comment.
Looks mostly good; a few things below.
| func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree) error { | ||
| prompt := utils.ResolvePlaceholderString(self.c.Tr.AlreadyCheckedOutByWorktree, map[string]string{ | ||
| func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree, branchName string) error { | ||
| title := utils.ResolvePlaceholderString(self.c.Tr.BranchCheckedOutByWorktree, map[string]string{ |
There was a problem hiding this comment.
I'm not a fan of putting this text in the title. Branch names can be very long, so this might get truncated. I see that we are doing the same thing already when deleting branches, but it's not good there, either. :)
I'd suggest to use a shorter title and put the longer text in a menu prompt (see CreateMenuOptions.Prompt), something like
Branch 'xyz' is checked out by worktree 'abc'. How would you like to proceed?
Switch to worktree
Detach worktree and checkout branch here
Cancel
|
|
||
| func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree) error { | ||
| prompt := utils.ResolvePlaceholderString(self.c.Tr.AlreadyCheckedOutByWorktree, map[string]string{ | ||
| func (self *BranchesController) promptToCheckoutWorktree(worktree *models.Worktree, branchName string) error { |
There was a problem hiding this comment.
This is a pretty long function; would be good to move it to BranchesHelper.
| if !worktree.IsPathMissing && !self.c.Git().Worktree.IsWorktreeClean(worktree.Path) { | ||
| detachDisabledReason = &types.DisabledReason{Text: self.c.Tr.DetachWorktreeHasUncommittedChanges} | ||
| } |
There was a problem hiding this comment.
I don't think we need this. Detaching a worktree when there are modified files doesn't lose data, the modifications will stay the same. And we don't guard against this when detaching a worktree in response to deleting a branch, either.
| }, | ||
| { | ||
| Label: self.c.Tr.DetachWorktree, | ||
| Tooltip: self.c.Tr.DetachWorktreeTooltip, |
There was a problem hiding this comment.
The tooltip sounds as if detaching the worktree is the only thing that will happen. It should also mention that the branch is going to be checked out in the current worktree afterwards.
7e7fa96 to
b66e3fa
Compare
|
Oh, and I force-pushed the branch because it had conflicts with master. |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | -3 |
TIP This summary will be updated as you push new changes. Give us feedback
PR Description
Please check if the PR fulfills these requirements
go generate ./...)When working with worktrees it's common to have a main project installation (e.g. running your database and services) alongside several worktrees for feature work. Previously, checking out a branch occupied by another worktree only offered "Switch to worktree", forcing you to switch to that worktree, detach it, then navigate back and check out the branch: a tedious round-trip.
This is the current behavior:

Now the prompt shows a menu with two options: "Switch to worktree" and "Detach worktree". The detach option runs


git checkout --detachon the other worktree and then checks out the branch in one step.The detach option is disabled when the other worktree has uncommitted changes, to prevent accidental loss of work.
