4

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

拥有当前的远程分支:

$ 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)

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

4

1 回答 1

5

如果本地分支和远程分支具有相同的名称,则本地分支只能跟踪远程分支以进行推送。对于拉取,本地分支不必与远程分支具有相同的名称。有趣的是,如果您设置一个分支 'foo' 来跟踪远程分支 'origin/bar'(使用 'git branch foo origin/bar')并且如果远程存储库当前没有一个名为 '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 最初应该从 master 拉/推,我们最终 br1 从 origin/master 拉并推到一个我们不知道存在的分支。

于 2012-04-07T04:16:03.897 回答