jul
jul

Reputation: 37484

How to track a remote git branch?

I have two branches, master and mybranch in a git repository. In a local folder cloned from the git repository before adding the second branch, I can only see the first branch, as expected:

xxx$ git branch
* master

In an attempt to fetch the other branch I tried

xxx$git branch --track mybranch

which returns

Branch origin/mybranch set up to track local branch master

which is not what I want, because from what I understand now my local branch mybranch is the same as master (right?).

So I have 2 questions:

EDIT I don't want to checkout mybranch, I just want to fetch.

EDIT 2

In the workspace w2 in which I created the branch (I cloned the repos, created mybranch and pushed it:

$ git branch -vv
  master    c417738 [origin/master] <latest commit msg>
* mybranch 0c7dbac <latest commit msg>

In the initial workspace w1 (where I first pushed master)

$ git branch -vv
* master 66bb412 <latest commit msg>

Now if I do a fetch in w1:

$ git fetch

I still have only master:

$ git branch -vv
* master 66bb412 <latest commit msg>

But if I check my remote branches, both master and mybranch exist:

$ git branch -r
  origin/master
  origin/mybranch

So fetch did not get the data from all branches, as I expected...

EDIT 3

Now, following Klas Mellbourn's instructions, I did:

git branch mybranch

And added these lines to my conf file:

[branch "mybranch"]
remote = origin
merge = refs/heads/mybranch

And now checking my branches I get:

$ git branch -vv
* master    66bb412 <latest commit msg>
  mybranch 66bb412 [origin/mybranch: ahead 3, behind 8] <latest commit msg from master (?)>

Why is the commit specified for mybranch the same as the one specified for master? And why does it specify the same commit number as the master's one?

Upvotes: 7

Views: 11253

Answers (1)

Klas Mellbourn
Klas Mellbourn

Reputation: 44437

I think you actually probably did

git branch --track origin/mybranch

Which created a local branch confusingly named origin/mybranch that tracks the local branch you were currently on

Delete that branch

git branch -d origin/mybranch

To get the remote branch simply do

git checkout mybranch

Which should return

Branch mybranch set up to track remote branch mybranch from origin.
Switched to a new branch 'mybranch'

If it does not, you can do

git checkout -b mybranch
git branch -u origin/mybranch

(Or the more succinct git checkout -t origin/mybranch that VonC suggests in his answer)

If it is important to you not to check out the new branch, you can do

git branch mybranch
git branch -u origin/mybranch mybranch

If you are using git 1.7 (which does not have the -u switch) and you absolutely do not want to checkout the tracking branch, I don't think you can create it using the command line, but you can edit the config file directly

git branch mybranch
git config --local --edit

Then add these lines

[branch "mybranch"]
    remote = origin
    merge = refs/heads/mybranch

Tip: you can study the output of git branch -vva to get an understanding of your branches:

Upvotes: 10

Related Questions