222

I have already got a local master branch tracking the remote master branch of a github project. Now, a collaborator of mine has created a new branch in the same project, and I want to do the following accordingly:

  1. create a new branch locally
  2. make this new branch track the newly create remote branch.

How should I do it properly?

MLister
  • 10,022
  • 18
  • 64
  • 92
  • 2
    git checkout --track -b / also works. – MLister Jun 29 '12 at 13:57
  • 16
    ... or just `git checkout -t /`. If there's only one remote-tracking branch that ends in `` you can even just do `git checkout ` and git guesses what you mean. – Mark Longair Jun 29 '12 at 14:01
  • See also [How do you make an existing Git branch track a remote branch?](http://stackoverflow.com/q/520650/456814). –  May 23 '14 at 18:25

7 Answers7

299
git fetch
git branch --track branch-name origin/branch-name

First command makes sure you have remote branch in local repository. Second command creates local branch which tracks remote branch. It assumes that your remote name is origin and branch name is branch-name.

--track option is enabled by default for remote branches and you can omit it.

max
  • 33,369
  • 7
  • 73
  • 84
  • 3
    When I do this, I get a warning: refname 'branch-name' is ambiguous. – Ultrasaurus Jan 14 '14 at 13:53
  • 20
    If there is only one remote branch with that branch name, you can just do `git checkout ` and git will automatically check it out and setup a tracking branch. Just wanted to reiterate what @Mark Longair said above in this comment: http://stackoverflow.com/questions/11262703/how-to-track-a-new-remote-branch-created-on-github#comment14806580_11262703. – Ryan Walls Jan 23 '14 at 15:39
  • The name of my remote branch is `remotes/origin/develop`? Shouldn't it be just `origin/develop`? Are these the same? – Incerteza Jun 12 '14 at 03:25
  • 3
    I had to do `git fetch --all`, otherwise new branches would not be fetched – mschrimpf Dec 31 '18 at 21:01
  • How to do this in Eclipse? When I'm done with those `cmd`, eclipse detect the new branch? – Deckard Apr 25 '19 at 05:18
  • 1
    Echoing @mschrimpf, for a special remote (e.g. `company_remote`) I had to first fetch from that remote via `git fetch company_remote` – Zephaniah Grunschlag Jun 08 '22 at 14:22
  • Use `git branch -r` to list all branches including currently remote-only branches – Teddy Jul 13 '22 at 04:33
  • The answer should contain that you have to do `git fetch --all` or `git fetch ` and not just `git fetch`, which seemingly does not do anything at all: the newly created remote will not get fetched, as `git branch -a -v` will show. – Nils Lindemann Sep 25 '22 at 20:57
49

If you don't have an existing local branch, it is truly as simple as:

git fetch
git checkout <remote-branch-name>

For instance if you fetch and there is a new remote tracking branch called origin/feature/Main_Page, just do this:

git checkout feature/Main_Page

This creates a local branch with the same name as the remote branch, tracking that remote branch. If you have multiple remotes with the same branch name, you can use the less ambiguous:

git checkout -t <remote>/<remote-branch-name>

If you already made the local branch and don't want to delete it, see How do you make an existing Git branch track a remote branch?.

Community
  • 1
  • 1
kotoole
  • 1,646
  • 1
  • 15
  • 16
  • 2
    `git checkout 1.5-branch` (`remotes/upstream/1.5-branch` is in the output of `git branch -a`) results in `error: pathspec '1.5-branch' did not match any file(s) known to git.` Changing this to `git checkout upstream/1.5-branch` results in detached HEAD and no local branch is created. I think this part of answer is simply wrong. This is with git 2.4.3 – Piotr Dobrogost Oct 29 '15 at 10:53
  • The first command works for me for me in git 1.9.3, even for different remotes. It's possible this behavior has changed. The result of your second command is what I'd expect. Without -t, you're not specifying that you want to do anything other than look at that specific version on the remote. – kotoole Oct 29 '15 at 15:06
  • 1
    I concur with @PiotrDobrogost, I believe your statement "This creates a local branch with the same name ..." is mistaken. What fetch *will* do (when called this way) is create a *remote tracking branch* for each branch on the remote. That means a branch on your local repo named `/`. A remote tracking branch is read-only, indicating where that branch was on the remote at last fetch. Calling `checkout` on that branch ref puts you in detached HEAD mode, just as Piotr says. Note that specifying the remote branch to fetch avoids creating unwanted remote tracking branches. – scanny Feb 28 '16 at 00:39
  • 2
    To be clear once more: I am suggesting using the command `git checkout feature-branch`. I am NOT suggesting using the command `git checkout origin/feature-branch` which, as you both have pointed out, will result in an unhelpful detached head. – kotoole Feb 29 '16 at 22:07
  • much simpler and to the point. Works fine with git 2.8.3 on cygwin. – Felipe Alvarez Mar 18 '17 at 13:56
  • Use `git branch -r` to list all branches including currently remote-only branches to make sure branch got fetched. Then checkout. – Teddy Jul 13 '22 at 04:34
36

First of all you have to fetch the remote repository:

git fetch remoteName

Than you can create the new branch and set it up to track the remote branch you want:

git checkout -b newLocalBranch remoteName/remoteBranch

You can also use "git branch --track" instead of "git checkout -b" as max specified.

git branch --track newLocalBranch remoteName/remoteBranch
Anghel Contiu
  • 371
  • 2
  • 3
  • If you do `checkout -b` using a remote branch as a starting point, then it's actually unnecessary to then use `--track`. –  May 23 '14 at 18:24
  • 6
    Also, as pointed out in [this comment](http://stackoverflow.com/questions/11262703/how-to-track-a-new-remote-branch-created-on-github#comment32122710_11262780), if you just do `git checkout `, and there is a remote branch with the same name (after you did a `git fetch`), then the local branch will automatically be set up to track the remote one. –  May 23 '14 at 18:30
9

When the branch is no remote branch you can push your local branch direct to the remote.

git checkout master
git push origin master

or when you have a dev branch

git checkout dev
git push origin dev

or when the remote branch exists

git branch dev -t origin/dev

There are some other posibilites to push a remote branch.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • Then you take the third option. When the remote branch exists you can create a local branch and track the remote branch :) its the same as the version from max but shorter. – René Höhle Jun 29 '12 at 13:47
2

Steps as listed below:

  1. First get all the branches that were created after cloning.
git fetch
  1. Now, check what are remote branches
git branch -r
  1. Check where you're in the log history
git log --oneline --all --graph
  1. Assign a new branch to track the remote branch
git branch branch_name origin/remote_branch_name
  1. After that, check your log history either using the step 3 command or git branch
desertnaut
  • 57,590
  • 26
  • 140
  • 166
1

I always use this way:

git fetch

then :

git checkout -b branchName origin/branchName
parastoo
  • 2,211
  • 2
  • 21
  • 38
0
git checkout master
git pull origin master

git checkout -b new-branch-name

//Set up tracking for the new local branch to the collaborator's remote branch:
git push -u origin new-branch-name
Ubaid Ali
  • 19
  • 3
  • 1
    There are **seven existing answers** to this question, including a top-voted, accepted answer with nearly **three hundred votes**. Are you _certain_ your solution hasn't already been given? If not, why do you believe your approach improves upon the existing proposals, which have been validated by the community? Offering an explanation is _always_ useful on Stack Overflow, but it's _especially_ important where the question has been resolved to the satisfaction of both the OP and the community. Help readers out by explaining what your answer does different and when it might be preferred. – Jeremy Caney Aug 12 '23 at 00:03
  • My response provided an approach to address the situation you described, but it's important to acknowledge that Git offers various methods to handle diverse issues, tailored to specific scenarios. My answer doesn't imply a fixed approach, as Git's flexibility allows for multiple strategies to tackle different problems. – Ubaid Ali Aug 12 '23 at 16:50