2

我的设置是:

$ 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 上,我添加了一个新文件,提交并推送。现在我签出“测试”分支并发出 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' 分支中。

到底是怎么回事?

4

2 回答 2

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' 配置的本地分支:”行下列出分支。因此,当您在分支“test”上时,案例 3 必须匹配“origin/test2”。

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

$ git branch --set-upstream test origin/test
$ <similar for test2>
于 2012-04-18T15:29:39.387 回答
0

git pull相当于运行git fetch & git merge您不向它传递任何参数,因此它连接到原点并获得两个分支并将其合并到您当前的分支中。

您可以git fetch & git merge origin/test1在本地 test1 分支上使用 while ,也可以使用git pull origin test1. 我更喜欢第一个,因为我可以在获取时看到远程发生的变化。

于 2012-04-18T14:20:10.147 回答