git connect my local branch with remote

Viewed 23536

In my local repo if I execute

$git branch --all

it returns

* master
  remotes/origin/develop
  remotes/origin/master

that means that I have a local branch named repo and 2 remote branches.

If I do

$git checkout -b develop

it will create a local branch that is unrelated to remote branch .

The command

$git pull origin develop

it will connect my local branch with remote branch?

3 Answers

The answer in accepted answer is changed and the updated step git branch --set-upstream-to origin/master did not work for me. (see Footnote below for error)

Solution: Below is what made it possible for me to track the develop branch.

Step 1: Check your branches (local and remotes using --all option).

D:\poseidon>git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/develop

So currently there is only one local branch (master) and two remote branches (master and develop).

Step 2: From this checked out master branch (and without creating a local develop branch); run below command.

D:\poseidon>git branch --track develop remotes/origin/develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.

D:\poseidon>git branch
  develop
* master

D:\poseidon>git checkout develop
Switched to branch 'develop'
Your branch is up to date with 'origin/develop'.

As you can see the last statement says your local branch develop is up to date with origin develop. You can now pull/push* from this branch.

*Although you may want to push to develop only using PRs and not directly. But it is now tracked and pull/push can be done.

Footnote:

D:\poseidon>git branch --set-upstream-to develop remotes/origin/develop
fatal: branch 'remotes/origin/develop' does not exist
Related