【问题标题】:Git Merging and Reparenting between 2 develop branches2个开发分支之间的Git合并和重新分配
【发布时间】:2017-11-09 23:34:33
【问题描述】:

我刚刚完成了 2 个独立的开发分支的工作,这些分支最初是从主主分支分支出来的。这是我在从主分支分支以创建这 2 个开发分支时使用的流程:

Master
  |--> DevelopBranch1
           |
            -->DevelopBranch2

所以最初我通过从Master 分支创建DevelopBranch1。然后我通过分支DevelopBranch1 创建了DevelopBranch2。我做出这个决定的原因是因为DevelopBranch2 取决于我在DevelopBranch1 中所做的新更改。

我已完成对 DevelopBranch1 的更改。由于DevelopBranch2 最初不是从Master 分支出来的,所以我现在想做一些事情:

1) 将DevelopBranch1 合并为Master

2) 将DevelopBranch2 重新映射到Master

3) 将DevelopBranch2 合并为Master

4) 存档DevelopBranch1

我成功地将DevelopBranch1 合并到Master 中,方法是使用SourceTree 签出Master 并将其与DevelopBranch1 的最新提交合并。

当我进入第 2 步时,这就是我遇到问题的地方。我使用了以下 git 命令来重新设置父级:

git checkout master
git rebase --onto DevelopBranch2

在 rebase 命令之后,我注意到在 SourceTree 中来自Master 的最新提交被移动到了DevelopBranch2 中的最新提交。

此时,我尝试将Master 合并到DevelopBranch2。使用此 Merge 并运行 git status 后,我收到以下消息

On branch master
Your branch and 'origin/master' have diverged,
and have 5 and 7 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

然后我运行git pull 并收到此消息:

git pull
error: Pulling is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

这是我目前卡住的地方。

任何帮助将不胜感激!

【问题讨论】:

  • 除非您有一个非常古老的 Git(例如 1.7.x),否则 git status 输出和随后对未合并文件的抱怨似乎无法正确混合。如果您确实拥有如此古老的 Git,那么这一切都说得通。
  • @torek 我能够通过使用git reset --hard master 恢复此更改,感谢这篇文章:stackoverflow.com/questions/2452226/… 我认为我遇到的问题是正确使用git rebase 来重新设置。跨度>
  • 一般来说,你要非常小心git reset --hard。我应该问的是:你运行的是什么版本的 Git?
  • @torek git --version git 版本 2.14.3.windows.1
  • 好的,那么我很困惑,因为git status 应该显示正在阻止git pull 的合并进行中。这可能是某些特定于 Windows 的东西。

标签: git merge version-control


【解决方案1】:

这是由错误使用命令git rebase --onto DevelopBranch2引起的。让我们用下图来说明:

step1之后(将DevelopBranch1合并为Master),命令历史为:

...---A---B---C-------J       Master
           \         /
            D---F---G    DevelopBranch1
                 \
                  H--I   DevelopBranch2

如果您使用命令git rebase --onto DevelopBranch2,它会将当前分支Master重置为DevelopBranch2,如下所示:

...---A---B---C-------J      
           \         /
            D---F---G    DevelopBranch1
                 \
                  H--I   DevelopBranch2, Master

对于您的情况,您应该改用 git rebase --onto Master DevelopBranch1 DevelopBranch2。然后它将按照您的预期将DevelopBranch2 重新设置为Master 分支。

首先,您应该将您的Master 分支重置为合并提交(该提交将DevelopBranch1 合并为Master),然后再次执行步骤2 的命令。使用的命令如下:

git checkout Master
git reset --hard <merged commit id>
git rebase --onto Master DevelopBranch1 DevelopBranch2
git push origin DevelopBranch2 -f

那么 step2 将被正确执行(如下图)。并且可以继续执行step3和step4。

                        H'---I'  DevelopBranch2
                       /
...---A---B---C-------J   Master
           \         /
            D---F---G    DevelopBranch1

【讨论】:

  • 这正是我所需要的!这是一个很好的解释,我非常感谢您花时间提供详细信息。我希望这对遇到这个问题的其他人也有帮助!
猜你喜欢
  • 2015-07-19
  • 1970-01-01
  • 2021-11-13
  • 2011-03-25
  • 1970-01-01
  • 2019-03-11
  • 2018-03-28
  • 2016-02-06
  • 1970-01-01
相关资源
最近更新 更多