Make an existing Git branch track a remote branch?

前端 未结 22 2991
执念已碎
执念已碎 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 07:57

    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.

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

    You might find the git_remote_branch tool useful. It offers simple commands for creating, publishing, deleting, tracking & renaming remote branches. One nice feature is that you can ask a grb command to explain what git commands it would execute.

    grb explain create my_branch github
    # git_remote_branch version 0.3.0
    
    # List of operations to do to create a new remote branch and track it locally:
    git push github master:refs/heads/my_branch
    git fetch github
    git branch --track my_branch github/my_branch
    git checkout my_branch
    
    0 讨论(0)
  • 2020-11-21 08:01

    For git version 2.25.1, use the command:

    git push --set-upstream origin <local_branch_name>
    
    0 讨论(0)
  • 2020-11-21 08:01

    This would work too

    git branch --set-upstream-to=/< remote>/< branch> < localbranch>
    
    0 讨论(0)
  • 2020-11-21 08:03

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

    For creating new branch, we could use following command

     git checkout --track -b example origin/example 
    For the already created branch to create link between remote then from that branch use below command

     git branch -u origin/remote-branch-name

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