12

I have a remote branch remote-branch-long-name. When I magit checkout with helm completion I can type remote-branch and origin/remote-branch-long-name will show up as an option. The issue is that to checkout this branch locally, I actually cannot switch to that branch, I need to switch to remote-branch-long-name (without the origin/ prefix), since helm doesn't provide that as a completion option, I have to manually type the entire branch name out.

Is there another function I should be using for this use case or do I have to hack a function together?

21

It sounds like the problem you are trying to solve is not the problem you are encountering. Git doesn't allow you to work locally with remote branches (the origin/remote-branch-long-name you see is just a tracking branch; you can't work with it directly) and Magit is guiding you toward the intended workflow.

Since you want to work with origin/remote-branch-long-name you should probably run magit-branch-and-checkout (b c). Select origin/remote-branch-long-name and it will prompt you for a local branch name and will default to "remote-branch-long-name". This gives you the local branch need to make your changes. When are done with your changes and push to origin/remote-branch-long-name it will update the remote tracking branch.

You can have local branches with a slash in them, but using a remote name in a local branch name can make things very confusing. Git remembers which remote a local branch was checked out from, so you don't need to track it yourself.

For a more complete explanation than my answer here (!) you find this page a to be good overview about remotes.

  • 5
    I think the disconnect here is that the Magit flow is different from the git flow. If you run git checkout foo, and foo is not a branch, but origin/foo is, git will create a new branch foo and set it to track origin/foo. So git teaches you to think "I just checkout foo, and git does everything necessary". But this does not work with magit; running magit-branch-and-checkout is the way to go. – zck Feb 22 '18 at 20:56
4

Since magit v2.12.0, there's a command magit-branch-checkout, bound to b l (Checkout local branch), that does exactly what you want. See the doc for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.