Make an existing Git branch track a remote branch?

2018-12-31 01:47发布

I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?

I know I can just edit the .git/config file, but it seems there should be an easier way.

19条回答
临风纵饮
2楼-- · 2018-12-31 02:18

Make sure you run :

git config push.default tracking

to be able to push trouble free

查看更多
琉璃瓶的回忆
3楼-- · 2018-12-31 02:21

I believe that in as early as Git 1.5.x you could make a local branch $BRANCH track a remote branch origin/$BRANCH, like this.

Given that $BRANCH and origin/$BRANCH exist, and you've not currently checked out $BRANCH (switch away if you have), do:

git branch -f --track $BRANCH origin/$BRANCH

This recreates $BRANCH as a tracking branch. The -f forces the creation despite $BRANCH existing already. --track is optional if the usual defaults are in place (that is, the git-config parameter branch.autosetupmerge is true).

Note, if origin/$BRANCH doesn't exist yet, you can create it by pushing your local $BRANCH into the remote repository with:

git push origin $BRANCH

Followed by the previous command to promote the local branch into a tracking branch.

查看更多
看风景的人
4楼-- · 2018-12-31 02:22

Given a branch foo and a remote upstream:

As of Git 1.8.0:

git branch -u upstream/foo

Or, if local branch foo is not the current branch:

git branch -u upstream/foo foo

Or, if you like to type longer commands, these are equivalent to the above two:

git branch --set-upstream-to=upstream/foo

git branch --set-upstream-to=upstream/foo foo

As of Git 1.7.0:

git branch --set-upstream foo upstream/foo

Notes:

All of the above commands will cause local branch foo to track remote branch foo from remote upstream. The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.


See also: Why do I need to do `--set-upstream` all the time?

查看更多
零度萤火
5楼-- · 2018-12-31 02:22

For 1.6.x, it can be done using the git_remote_branch tool:

grb track foo upstream

That will cause Git to make foo track upstream/foo.

查看更多
美炸的是我
6楼-- · 2018-12-31 02:24

1- update your local meta-data using : git fetch --all

enter image description here

2- show your remote and local branches using : git branch -a , see the following Screenshot

enter image description here

3- switch to target branch , that you want to linked with the remote: using

git checkout branchName

example :

enter image description here

4- Link your local branch to a remote branch using:

git branch --set-upstream-to nameOfRemoteBranch

N.B : nameOfRemoteBranch : to copy from the output of step 2 " git branch -r "

Example of use:

enter image description here

查看更多
大哥的爱人
7楼-- · 2018-12-31 02:27

I use the following command (Suppose your local branch name is "branch-name-local" and remote branch name is "branch-name-remote"):

$ git branch --set-upstream-to=origin/branch-name-remote branch-name-local

If both local and remote branches have the same name, then just do the following:

$ git branch --set-upstream-to=origin/branch-name branch-name
查看更多
登录 后发表回答