Make an existing Git branch track a remote branch?

前端 未结 22 2945
执念已碎
执念已碎 2020-11-21 07:16

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

相关标签:
22条回答
  • 2020-11-21 08:04

    For anyone who, like me, just wants to sync up your local branch name with the remote branch name, here's a handy command:

    git branch -u origin/$(git rev-parse --abbrev-ref HEAD)
    
    0 讨论(0)
  • 2020-11-21 08:07

    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
    
    0 讨论(0)
  • 2020-11-21 08:08

    Make sure you run :

    git config push.default tracking
    

    to be able to push trouble free

    0 讨论(0)
  • 2020-11-21 08:09

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

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

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

    git checkout branchName

    example :

    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:

    0 讨论(0)
  • 2020-11-21 08:14

    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.

    0 讨论(0)
  • 2020-11-21 08:14

    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.

    0 讨论(0)
提交回复
热议问题