Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow remote branch checkout-by-name without detaching #3371

Closed
wants to merge 1 commit into from

Conversation

jesseduffield
Copy link
Owner

@jesseduffield jesseduffield commented Mar 9, 2024

Fixes #2312

The idea here is that in the git CLI you will often want to checkout a remote branch like origin/blah by doing git checkout blah. That will automatically create a local branch named blah that tracks origin/blah.

Previously in the suggestions view when checking out a branch by name, we would only show local branches and remote branches like origin/blah but wouldn't show blah as an option (assuming no local branch existed with that name). This meant users would checkout origin/blah and git would check out that branch as a detached head which is rarely what you actually want.

Now we give them both options. The alternative approach we could have taken is to still show the branch as origin/blah but then ask if the user wants to check out the branch as detached or as a local branch tracking the remote branch. That approach is certainly more versatile, but it's also something you can do already by going to the remote branch directly via the remotes view. Admittedly, my approach involves less work.

  • PR Description

  • Please check if the PR fulfills these requirements

  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see here for the integration test guide)
  • Text is internationalised (see here)
  • Docs (specifically docs/Config.md) have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

The idea here is that in the git CLI you will often want to checkout a
remote branch like origin/blah by doing `git checkout blah`. That will
automatically create a local branch named blah that tracks origin/blah.

Previously in the suggestions view when checking out a branch by name,
we would only show local branches and remote branches like origin/blah
but wouldn't show blah as an option (assuming no local branch existed
with that name). This meant users would checkout origin/blah and git
would check out that branch as a detached head which is rarely what you
actually want.

Now we give them both options. The alternative approach we could have
taken is to still show the branch as origin/blah but then ask if the
user wants to check out the branch as detached or as a local branch
tracking the remote branch. That approach is certainly more versatile,
but it's also something you can do already by going to the remote branch
directly via the remotes view. Admittedly, my approach involves less
work.
@jesseduffield jesseduffield added the enhancement New feature or request label Mar 9, 2024
@jesseduffield jesseduffield changed the title Local-ise remote branches when showing suggestions for branch checkout Allow remote branch checkout-by-name without detaching Mar 9, 2024
Copy link

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.01% (target: -2.00%) 100.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (44f553b) 48531 40724 83.91%
Head commit (4a07e03) 48591 (+60) 40779 (+55) 83.92% (+0.01%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#3371) 64 64 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

You may notice some variations in coverage metrics with the latest Coverage engine update. For more details, visit the documentation

@stefanhaller
Copy link
Collaborator

Hm, I'm not sure. To be honest, I don't use the "checkout by name" command much because it's pretty much unusable for me with the amount of remote branches we have at work (more than 3000), and since filtering the suggestions doesn't sort them by date like the remote branches panel does, I get them in some seemingly arbitrary order, so it's too hard to find the branch I'm looking for. But adding all these local copies of the remote branches makes it even more unusable for me, especially since the remote branch and its local copy often don't show up next to each other in the suggestions list, making it pretty confusing.

I guess I'd prefer the other option of being asked whether I want a detached head or create a local branch.

@karimkhaleel
Copy link
Contributor

Maybe we can make it a user config? I have never wanted to checkout a detached head, so I would be cool with just setting a config that would always create a local branch and track a remote when checkout out a remote branch in the branch view.

@mark2185
Copy link
Collaborator

A bit of a different approach - when checking out a remote branch e.g. origin/copper the Local branches panel shows "(HEAD detached at origin/copper)", couldn't we just extract "copper" from that string and prefill the New branch popup with it?

That way it wouldn't be disruptive for people who are used to detached HEADs, and for people who want to checkout a remote branch locally they'd just have to press n<Enter> to get what they want.

@karimkhaleel
Copy link
Contributor

A bit of a different approach - when checking out a remote branch e.g. origin/copper the Local branches panel shows "(HEAD detached at origin/copper)", couldn't we just extract "copper" from that string and prefill the New branch popup with it?

I kind of like that idea. Played around a bit with it. Kind of like this? karimkhaleel@5279bd0

@stefanhaller
Copy link
Collaborator

A bit of a different approach - when checking out a remote branch e.g. origin/copper the Local branches panel shows "(HEAD detached at origin/copper)", couldn't we just extract "copper" from that string and prefill the New branch popup with it?

That way it wouldn't be disruptive for people who are used to detached HEADs, and for people who want to checkout a remote branch locally they'd just have to press n<Enter> to get what they want.

This doesn't really solve it, because that new branch wouldn't track the remote branch, so you'd also have to do the set upstream thing manually.

@stefanhaller
Copy link
Collaborator

The alternative approach we could have taken is to still show the branch as origin/blah but then ask if the user wants to check out the branch as detached or as a local branch tracking the remote branch.

I prefer this approach, so I made a PR that implements it: #3388.

@jesseduffield
Copy link
Owner Author

Closing in favour of #3388 (review)

stefanhaller added a commit that referenced this pull request Mar 17, 2024
- **PR Description**

When checking out a remote branch by name, ask the user how they want to
check it out; the choices are to create a new local branch that tracks
the remote, or a detached head.

This is an alternative to #3371, and fixes #2312.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Checkout by name without detaching
4 participants