git - 重新设置从主分支分支出来的分支的分支时要采取的步骤
问题描述
我一直在做噩梦,我认为这是因为我采取了错误的步骤。
我有 3 个分支 ... mainbranch、branch1、branch2 ... branch1 是 mainbranch 的一个分支 ... branch2 是 branch1 的一个分支
branch1 比 mainbranch 提前 1 次提交 branch2 比 branch1 提前 1 次提交
所以在这种状态下,当我... git fetch -p; 时遇到问题 git pull -r ... on mainbranch 我现在想将 mainbranch 上的新更改带入 branch1 和 branch2。这就是我目前正在做的事情:
git checkout mainbranch
git fetch -p
git pull -r
git checkout branch1
git rebase -i mainbranch (i dont squash commits or anything like that)
git checkout branch2
git rebase -i branch1 <<< this causes the problem
当我运行最后一个命令时,我做了一场冲突的噩梦。我假设是因为不知何故,当我在上述步骤中进行第一次变基时,它弄乱了提交哈希并且混淆了 git。我应该采取哪些步骤来正确执行此操作?谢谢
这里的目标是最终处于初始状态(分支 1 彼此提前提交),但所有分支中的所有更改都来自 mainbranch
解决方案
pull -r
rebase 来自你的新提交mainbranch
,所以他们真的得到了新的哈希,这使得 git 以不同的方式识别它们。我不确定有多少文本更改会影响此行为(我猜他们做的最多),但这(仅?)可能会在修改相同文件的重新提交时发生。你只需要告诉 git用它们的单个branch1
提交来重新设置和branch2
分支(我假设这从你的问题中很清楚),因为 git 本身不会跟踪它们。
git checkout mainbranch
git fetch -p
git pull --rebase
# rebase branch 1
git rebase --onto mainbranch branch1~1 branch1
# rebase branch 2
git rebase --onto branch1 branch2~1 branch2
请注意,变基命令告诉 git 根据以下规则开始变基:
--onto <base>
告诉 git 从哪里开始构建新的提交到 (--onto mainbranch
然后--onto branch1
)<upstream>~1
告诉 git 要应用的补丁的范围/数量(实际上只有一个)<downstream>
告诉 git 分支名称变基
因此,它类似于“git 请将分支重新定位branch1
到当前mainbranch
,请只使用一个 ( ~1
) 补丁”,然后对于 branch1-branch2 重新定位也是如此。
使用交互式 rebase 时可能更容易找出它发生的原因(至少你可以看到是什么原因造成的):
git checkout mainbranch
git fetch -p
git pull --rebase
# rebase branch 1
git checkout branch1
git rebase -i mainbranch
# note that the editor opens with more than one command: two or more picks (depends on the number of commits on main branch)
# simply remove all commands except the last `pick` command that picks the only commit from the branch1 branch
# rebase branch 2
git checkout branch2
git rebase -i branch1
# the same here: note that this will also include the original commit from branch1 that should be dropped too
# keep the last `pick` command only
一旦完成,在这两种情况下(你决定你喜欢遵循哪个),git log --oneline
都会产生类似的结果:
CCCCCC (branch2) the only commit from branch2
BBBBBB (branch1) the only commit from branch1
AAAAAA (mainbranch) the tip commit at mainbranch
...
如果您熟悉那里发生的事情,并且您对做 rebase 感到安全,并且了解可能会出现问题,那么您甚至可以执行以下操作:
git checkout mainbranch
git fetch -p
git pull --rebase
# rebase branch 1
git rebase master
git rebase --skip # assuming conflicts will occur here
# and this is equivalent to dropping the current patch from the interactive rebase (scenario 2 above)
# or excluding the commit from the commit range (scenario 1 above)
# possibly more `git rebase --skip` (unless the only patch is applied)
# rebase branch 2
git rebase branch1
git rebase --skip # one time than the previous rebase because of the old "branch1 commit on branch2" commit
我更喜欢最后一个,因为我可以在我的 bash 提示符中看到它(要应用的补丁数量、rebase 完成后的提前/落后等),但我总是git log
在强制推送之前仔细检查结果。
推荐阅读
- javascript - jQuery:表单提交 - 多个事件处理程序
- searchkick - 在 searchkick 中获取已加载记录中的 ElasticSearch 文档字段
- javascript - Html如何将数据从一页发送到另一页
- linux - linux中文件大小增加的进度条
- java - 如何将 html 数据转换为 json 对象?
- flutter - 错误:'package:flutter/src/widgets/media_query.dart':断言失败:第 715 行 pos 12:'context != null':不正确
- ios - Swift 持久化并检索 UITableview 单元格复选标记
- git - 将 git 中的分支恢复为同一分支中的上一个提交
- html - 将文本与图像在同一水平对齐
- drupal - Drupal 8 自定义表单 managed_file 多个上传字段限制上传文件的数量