26

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?

irregular
  • 962
  • 1
  • 9
  • 21

2 Answers2

39

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.

tarsius
  • 25,298
  • 4
  • 69
  • 109
ebpa
  • 7,319
  • 26
  • 53
  • 16
    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
13

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.

Emoses
  • 573
  • 5
  • 8
  • I did `F p` but I don't see `origin/name-of-my-branch` when I do `b c`. After running the command `git pull origin` I'm able to `b l` to it. Why does pulling through magit not work the same way as pulling through git? – Boris Verkhovskiy Feb 13 '21 at 16:01
  • Assuming that `origin` *is* your push remote... You've used two different commands. `b c` and `b l` read the branch argument differently. – phils Feb 17 '21 at 08:38
  • what I'm saying is that I have the branch pushed up on github but I don't have it locally. neither command lets me use it's name until I pull using the git command. How do I pull it through magit? – Boris Verkhovskiy Feb 17 '21 at 11:25
  • 1
    `F p` only fetches the branch you're pulling from (you'll see from the process log that Magit passes the `` argument). Use `f p` to do a general `git fetch`, and then `b l` will know what `origin/name-of-my-branch` is. – phils Feb 18 '21 at 05:01
  • `git pull` on the command line also fetching other branches by default is a slightly weird quirk, so using that command purely for that side-effect is an equally quirky workflow. If fetching *other* branches is the goal, then `git fetch` is the appropriate command. – phils Feb 18 '21 at 05:07