简体   繁体   中英

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?

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

git branch --set-upstream develop origin/develop

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM