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:29

Editing .git/config is probably the easiest and fastest way. That's what the Git commands for handling remote branches are doing, anyway.

If you don't want to muck with the file by hand (and it's not that hard to do), you can always use git config to do it...but again, that's just going to edit the .git/config file, anyway.

There are, of course, ways to automatically track a remote branch when using git checkout (by passing the --track flag, for example), but these commands work with new branches, not existing ones.

查看更多
人间绝色
3楼-- · 2018-12-31 02:29

or simply by :

switch to the branch if you are not in it already:

[za]$ git checkout branch_name

run

[za]$ git branch --set-upstream origin branch_name
Branch origin set up to track local branch brnach_name by rebasing.

and you ready to :

 [za]$ git push origin branch_name

You can alawys take a look at the config file to see what is tracking what by running:

 [za]$ git config -e

It's also nice to know this, it shows which branches are tracked and which ones are not. :

  [za]$ git remote show origin 
查看更多
栀子花@的思念
4楼-- · 2018-12-31 02:31

You can do the following (assuming you are checked out on master and want to push to a remote branch master):

Set up the 'remote' if you don't have it already

git remote add origin ssh://...

Now configure master to know to track:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

And push:

git push origin master
查看更多
人间绝色
5楼-- · 2018-12-31 02:31

Use '--track' Option

  • After a git pull :

    git checkout --track <remote-branch-name>

  • Or:

    git fetch && git checkout <branch-name>

查看更多
明月照影归
6楼-- · 2018-12-31 02:36

In very short

git branch --set-upstream yourLocalBranchName origin/develop

This will make your yourLocalBranchName track the remote branch called develop.

查看更多
明月照影归
7楼-- · 2018-12-31 02:36

This would work too

git branch --set-upstream-to=/< remote>/< branch> < localbranch>
查看更多
登录 后发表回答