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

时间:2021-04-11 23:42:53

When creating a branch with name that matches that of a remote branch the configurations for push and pull are set differently.

创建名称与远程分支名称匹配的分支时,push和pull的配置设置不同。

Having the current remote branches:

拥有当前的远程分支:

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

And creating a couple local branches tracking a remote branch:

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

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

Checking the tracking and upstream information:

检查跟踪和上游信息:

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

Why the second branch was created without the push configuration?

为什么没有推送配置创建第二个分支?

1 个解决方案

#1


4  

A local branch can only track a remote branch for pushing if the local and remote branches have the same name. For pulling, the local branch need not have the same name as the remote branch. Interestingly, if you setup a branch 'foo' to track a remote branch 'origin/bar' (with 'git branch foo origin/bar') and if the remote repository does not currently have a branch named 'foo', then if later the remote does get a branch named foo, then the local branch foo will track origin/foo thereafter!

如果本地和远程分支具有相同的名称,则本地分支只能跟踪远程分支以进行推送。为了提取,本地分支不需要与远程分支具有相同的名称。有趣的是,如果您设置分支'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)

So br1 was originally supposed to pull/push from master and we end up with br1 pulling from origin/master and pushing to a branch we never knew existed.

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

#1


4  

A local branch can only track a remote branch for pushing if the local and remote branches have the same name. For pulling, the local branch need not have the same name as the remote branch. Interestingly, if you setup a branch 'foo' to track a remote branch 'origin/bar' (with 'git branch foo origin/bar') and if the remote repository does not currently have a branch named 'foo', then if later the remote does get a branch named foo, then the local branch foo will track origin/foo thereafter!

如果本地和远程分支具有相同的名称,则本地分支只能跟踪远程分支以进行推送。为了提取,本地分支不需要与远程分支具有相同的名称。有趣的是,如果您设置分支'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)

So br1 was originally supposed to pull/push from master and we end up with br1 pulling from origin/master and pushing to a branch we never knew existed.

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