2

How does one add a local branch from that of another developer? My colleague's branch is not out on origin.

Thanks!

hybrid9
  • 2,466
  • 4
  • 24
  • 24
  • 1
    I've taken a guess in my answer about what you're trying to do, but you really need to add more detail to the question for people to be able to do a good job of answering. For example, when you say that your "colleague's branch is not out on origin", it's not clear what that means - my assumption was that your colleague has a separate repository with the branch you want in it, but it would be good if you clarified that and added more detail. – Mark Longair Jun 29 '12 at 21:24

1 Answers1

4

First, add a "remote" (like a nickname for a repository URL) for your colleague's repository:

git remote add colleague <URL-of-their-repository>

Then fetch all the branches from that repository into remote tracking branches called refs/remotes/colleague/<branch-name> (which can usually be abbreviated to colleague/<branch-name>):

git fetch colleague

Now create (and switch to) a local branch called foo which tracks your colleague's branch called foo with:

git checkout --track colleague/foo
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Nice, but is this adding a repo to origin or local? Why git remote fetch colleague vs git pull? – hybrid9 Jun 29 '12 at 21:16
  • You can't "add a repo to origin" - `origin` is a remote that represents a repository, so I'm not sure what you mean by that. `git pull` does a `git fetch` and then a `git merge` into your current branch, but in this case it sounded like you wanted to create a local branch based on your colleague's branch, rather than immediately merging it into one of your branches, so it makes more sense to just do the fetch, and then create your local branch. – Mark Longair Jun 29 '12 at 21:20
  • Sorry, I've just corrected a typo - I meant just `git fetch colleague` – Mark Longair Jun 29 '12 at 21:22
  • `git checkout foo` will do instead if the branch name is unique. – Adam Dymitruk Jun 29 '12 at 22:27
  • 1
    @AdamDymitruk: indeed, but I tend not to suggest taking advantage of that bit of DWIM behaviour - I think it appears magic enough to be confusing! – Mark Longair Jun 30 '12 at 07:41
  • @MarkLongair Yes, exactly; I wanted to add a colleague's branch as a local branch first, then merge it into one of my other branches for a build. I should have been more clear, sorry. – hybrid9 Jun 30 '12 at 15:17