Git pull with multiple branches - 与错误的分支合并

时间:2012-04-18 14:13:00

标签: git git-pull

我的设置是:

$ git remote show origin
* remote origin
  Fetch URL: ssh://repo.xxx/project.git
  Push  URL: ssh://repo.xxx/project.git
  HEAD branch: master
  Remote branches:
    test  tracked
    test2 tracked
  Local refs configured for 'git push':
    test  pushes to test  (up to date)
    test2 pushes to test2 (up to date)

我在分支test2上,我添加了一个新文件,提交并推送。 现在我检查'test'分支并发出一个git pull:

touch file.txt
git add file.txt
git commit -m "file.txt"
git push

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 241 bytes, done.
Total 2 (delta 0), reused 0 (delta 0)
To ssh://repo.xxx/project.git
   98dd105..fbbd238  test2 -> test2

git checkout test
git pull

突然,'test2'分支的内容被合并到我当前的'test'分支中。

发生了什么事?

2 个答案:

答案 0 :(得分:2)

'git pull --help'的文档描述了如何确定要合并的远程分支:

  

在获取之后确定要合并哪个远程分支的规则有点涉及,以便不这样做          打破向后兼容性。

   If explicit refspecs were given on the command line of git pull, they are all merged.

   When no refspec was given on the command line, then git pull uses the refspec from the configuration
   or $GIT_DIR/remotes/<origin>. In such cases, the following rules apply:

    1. If branch.<name>.merge configuration for the current branch <name> exists, that is the name of
       the branch at the remote site that is merged.

    2. If the refspec is a globbing one, nothing is merged.

    3. Otherwise the remote branch of the first refspec is merged.

根据您的描述,案例1似乎不适用,因为如果它确实那么'git remote show ...'会在“为git pull'配置的本地分支”下列出分支:“行。因此,当您进行分支'测试'时,情况3必须匹配'origin / test2'。

当然,您可以通过明确本地和远程分支之间的映射来避免此问题。使用:

$ git branch --set-upstream test origin/test
$ <similar for test2>

答案 1 :(得分:0)

git pull相当于运行git fetch & git merge你没有传递任何参数,所以它连接到origin并获得两个分支将它合并到你当前的分支。

您可以在本地test1分支上使用git fetch & git merge origin/test1,也可以git pull origin test1。我更喜欢第一个,因为我可以看到在取出时遥控器上有什么变化。