MLister
MLister

Reputation: 10310

Track a new remote branch created on GitHub

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?

Upvotes: 230

Views: 203496

Answers (7)

satk0
satk0

Reputation: 524

Use GitHub website (fork sync a new branch)


NOTE:

All other answers solving the problem assumed you have the new branch inside your origin remote. For me, It wasn't the case as I have forked the upstream repo. So this answer shows how to set it up for a forked repository.


You just need to create a new branch that is in sync with a newly created branch on some upstream remote.

  1. Inside your fork repo, go to branches -> new branch tab:

Creating new branch from the one on the remote

In this example, I created a branch v2 in my fork repo in sync with a v2 branch on the satk0/logstash-exporter remote (the upsteam branch).

  1. After updating my fork repo, all I have to do is to pull the newly created branch to my local repo with simple:
git pull

Now, when I check my remote branches:

git branch -r

I finally have the origin/v2 remote branch and the fork is in sync with the v2 branch on the upstream repo.

  1. Now I just need to create a new branch in my local repo tracking the remote with:
git branch v2 origin/v2

Upvotes: 0

Chetan Patteparapu
Chetan Patteparapu

Reputation: 31

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

Upvotes: 3

parastoo
parastoo

Reputation: 2469

I always use this way:

git fetch

then :

git checkout -b branchName origin/branchName

Upvotes: 1

kotoole
kotoole

Reputation: 1746

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?.

Upvotes: 53

Anghel Contiu
Anghel Contiu

Reputation: 401

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

Upvotes: 39

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27325

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.

Upvotes: 9

max
max

Reputation: 34437

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.

Upvotes: 315

Related Questions