KostasA
KostasA

Reputation: 5570

git connect my local branch with remote

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?

Upvotes: 9

Views: 41385

Answers (3)

Cory Kramer
Cory Kramer

Reputation: 117866

If by "connect" you mean you want your local branch to track the remote branch, then you need to have your branch --set-upstream-to. Change to the branch you want to connect and perform

git branch --set-upstream-to remotes/origin/develop

Then things like git pull and git status will know which remote branch to track.

Upvotes: 24

Pramod Yadav
Pramod Yadav

Reputation: 557

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

Upvotes: 7

developer_hatch
developer_hatch

Reputation: 16224

If you want to do some changes locally and the push it to the branch on the repo, just do:

git checkout -b develop origin/develop

do some changes

git add -A

git commit -m "message, changes was made"

git push -u origin develop origin/develop

Git will set up the tracking information during the push.

Upvotes: 2

Related Questions