【问题标题】:Git pull with mutliple branches - merge with wrong branch带有多个分支的 Git pull - 与错误的分支合并
【发布时间】:2012-04-18 14:12:59
【问题描述】:

我的设置是:

$ 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'分支中。

发生了什么事?

【问题讨论】:

    标签: git git-pull


    【解决方案1】:

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

    fetch 后确定要合并哪个远程分支的规则有点涉及,为了不 打破向后兼容性。

       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>
    

    【讨论】:

    • --set-upstream 正是缺少的。谢谢!
    【解决方案2】:

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

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

    【讨论】:

    • FETCH_HEAD 的内容和这个有关系吗?里面有“not-for-merge”标记。
    • 我不确定你的意思,但 FETCH_HEAD 只是为了实现 git pull 因为它运行git fetch 然后git merge FETCH_HEAD
    • 那里列出了两个分支(test 和 test2),并且 test 添加了一个非合并选项。
    • 虽然使用git pull 似乎更容易,但git fetch &amp; git merge origin/branch 更好。无论如何,您问什么是不可合并,FETCH_HEAD 是一个将 sha1 与分支相关联的文件,文件中的行通过使用不可合并标记标记行来描述需要合并到当前 HEAD 中的提交。因此,如果您的 HEAD 在 test1 分支中,它会检查是否需要合并 test2 需要合并,因为它不在 HEAD
    猜你喜欢
    • 1970-01-01
    • 2012-06-21
    • 2012-04-30
    • 2013-03-05
    • 2021-08-18
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多