与远程同名的Git分支以不同方式创建

时间:2012-04-06 22:48:29

标签: git git-branch

创建名称与远程分支相匹配的分支时,推送和拉取的配置设置不同。

拥有当前的远程分支:

$ git branch -r
  origin/HEAD -> origin/master
  origin/master
  origin/someBranch

创建一些跟踪远程分支的本地分支:

$ git branch someBranch origin/someBranch 
  Branch someBranch set up to track remote branch someBranch from origin.
$ git branch someOtherBranch origin/someBranch 
  Branch someOtherBranch set up to track remote branch someBranch from origin.

检查跟踪和上游信息:

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

为什么第二个分支是在没有推送配置的情况下创建的?

1 个答案:

答案 0 :(得分:5)

如果本地和远程分支具有相同的名称,则本地分支只能跟踪远程分支以进行推送。为了提取,本地分支不需要与远程分支具有相同的名称。有趣的是,如果您设置分支'foo'来跟踪远程分支'origin / bar'(使用'git branch foo origin / bar'),并且远程存储库当前有一个名为branch的分支'foo',然后如果稍后遥控器确实得到一个名为foo的分支,那么本地分支foo将跟踪origin / foo!

$ git clone dev1 dev2
Cloning into 'dev2'...
done.
$ cd dev2
#
# After clone, local master tracks remote master.
#
$ git remote show origin
* remote origin
Fetch URL: /Users/ebg/dev1
Push  URL: /Users/ebg/dev1
HEAD branch: master
Remote branch:
  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)
#
# Create branch br1 to track origin/master
#
$ git branch br1 origin/master
Branch br1 set up to track remote branch master from origin.
#
# br1 tracks origin/master for pulling, not pushing
#
$ git remote show origin
* remote origin
Fetch URL: /Users/ebg/dev1
Push  URL: /Users/ebg/dev1
HEAD branch: master
Remote branch:
  master tracked
Local branches configured for 'git pull':
  br1    merges with remote master
  master merges with remote master
Local ref configured for 'git push':
  master pushes to master (up to date)
#
# Go to the origin repo and now create a new 'br1' branch
#
$ cd ../dev1
$ git checkout -b br1
Switched to a new branch 'br1'
#
# Go back to dev2, fetch origin
#
$ cd ../dev2
$ git fetch origin
From /Users/ebg/dev1
 * [new branch]      br1        -> origin/br1
#
# Now local branch 'br1' is tracking origin/br1 for pushing
#
$ git remote show origin
* remote origin
Fetch URL: /Users/ebg/dev1
Push  URL: /Users/ebg/dev1
HEAD branch (remote HEAD is ambiguous, may be one of the following):
  br1
 master
Remote branches:
  br1    tracked
  master tracked
Local branches configured for 'git pull':
  br1    merges with remote master
  master merges with remote master
Local refs configured for 'git push':
  br1    pushes to br1    (up to date)
  master pushes to master (up to date)

所以br1原本应该从主人那里拉/推,最后我们最终从原点/主人那里拉出br1并推到我们从未知道的分支。