Make an existing Git branch track a remote branch?

前端 未结 22 2992
执念已碎
执念已碎 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:15

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

    In very short

    git branch --set-upstream yourLocalBranchName origin/develop
    

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

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

    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 (before 1.8.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.
    • Defining an upstream branch will fail when run against newly-created remotes that have not already been fetched. In that case, run git fetch upstream beforehand.

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

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

    Here, using github and git version 2.1.4, just do:

    $ git clone git@github.com:user/repo.git
    

    And remotes come by itelsef, even if not linked locally:

    $ git remote show origin
    
    * remote origin
      Fetch URL: git@github.com:user/repo.git
      Push  URL: git@github.com:user/repo.git
      HEAD branch: master
      Remote branches:
        develop tracked         <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        master  tracked
      Local branch configured for 'git pull':
        master merges with remote master
      Local ref configured for 'git push':
        master pushes to master (up to date)
    

    But of course, still no local branch:

    $ git branch
    * master                  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    

    See? Now if you just checkout develp, it will do the magic automatically:

    $ git checkout develop
    Branch develop set up to track remote branch develop from origin.
    Switched to a new branch 'develop'
    

    So easy!


    Summary. Just run this 2 commands:

    $ git clone git@github.com:user/repo.git
    $ git checkout develop
    
    0 讨论(0)
提交回复
热议问题