Git 重新设置从主分支分支分支的分支的分支的基址时要采取的步骤

Git 重新设置从主分支分支分支的分支的分支的基址时要采取的步骤,git,Git,我不断得到噩梦般的回扣,我想这是因为我采取了错误的步骤 我有三个分支。。。主要分支,分支1,分支2。。。branch1是Main branch的一个分支。。。branch2是branch1的分支 branch1比mainbranch早1个提交 branch2比branch1早1次提交 所以在这种状态下,当我。。。git-fetch-p;git pull-r。。。主枝 现在,我想将mainbranch上的新更改引入branch1和branch2。这就是我目前正在做的: git checkout m

我不断得到噩梦般的回扣,我想这是因为我采取了错误的步骤

我有三个分支。。。主要分支,分支1,分支2。。。branch1是Main branch的一个分支。。。branch2是branch1的分支

branch1比mainbranch早1个提交 branch2比branch1早1次提交

所以在这种状态下,当我。。。git-fetch-p;git pull-r。。。主枝 现在,我想将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签出主分支
git-fetch-p
git pull-r
git签出分支1
git rebase-i mainbranch(我不挤压提交或类似的内容)
git签出分支2

git rebase-i branch1
pull-r
对来自您的
main分支的新提交进行重新设置,因此它们确实得到了新的哈希值,这使得git能够以不同的方式识别它们。我不确定文本更改对这种行为的影响有多大(我想它们影响最大),但当对修改相同文件的提交进行重定基时(仅?)会发生这种情况。您只需要告诉git使用它们的单个提交来重新设置
branch1
branch2
分支的基础(我假设这一点从您的问题中是清楚的),因为git本身并不跟踪它们

git签出主分支
git-fetch-p
git-pull——重基
#重订基期分行1
git rebase——在主分支branch1~1 branch1上
#重订基期分行2
git rebase——在branch1 branch2~1 branch2上
请注意,rebase命令告诉git根据以下规则开始重定基址:

  • --on
    告诉git在何处开始构建新提交(
    --mainbranch
    然后
    --branch1
  • ~1
    告诉git应用补丁的范围/数量(字面上只有一个)
  • 告诉git要重新设置基础的分支名称
因此,它类似于“git请将分支
branch1
重新设置到当前的
main分支上,请只设置一个(
~1
)补丁”,然后对分支1-branch2重新设置相同的位置

在使用交互式重基时,可能更容易找到发生这种情况的原因(至少您可以看到是什么原因造成的):

git签出主分支
git-fetch-p
git-pull——重基
#重订基期分行1
git签出分支1
git rebase-i主要分支机构
#请注意,编辑器打开时有多个命令:两个或多个拾取(取决于主分支上的提交数量)
#只需删除除最后一个“pick”命令之外的所有命令,该命令从branch1分支中拾取唯一的提交
#重订基期分行2
git签出分支2
git rebase-i branch1
#这里也是一样:请注意,这还将包括branch1的原始提交,该提交也应该被删除
#仅保留最后一个“pick”命令
一旦完成,在这两种情况下(您决定要遵循哪种方式),
git log--oneline
将产生如下结果:

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
CCCCC(branch2)branch2的唯一提交
BBBBBB(branch1)来自branch1的唯一提交
AAAAA(mainbranch)在mainbranch提交提示
...
如果你熟悉那里发生的事情,并且你觉得做回扣很安全,并且知道可能会出问题,那么你甚至可以做如下事情:

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提示符中看到它(要应用的补丁的数量,重基完成后的aheads/behinds,等等),但是在强制推之前,我总是使用git log反复检查结果。

如果您只是使用git rebase-r
而不是
-I
来代替
-I
许多thx,我会尝试一下,如果它有效,我会接受的。我相信它会:)我已经测试了第一种方法,它工作得非常好谢谢,第二个重基是平滑的:)我假设~1表示该分支比另一个分支先提交1次?我也会尝试其他方法,希望能找到其中一种——非常感谢您的帮助:)PS喜欢在本地检查git日志的技巧,这与强制之前的预期一样push@danday74我假设~1表示该分支比另一个分支先提交1次否,它表示从中获取修补程序的提交范围:您可以使用git log检查要应用的修补程序的范围:
git log branch~1..branch
——它将精确显示一个修补程序,以及唯一要重新基于另一个分支的修补程序。因此,基本上,您只需告诉git要做什么(交互式重新基址是可视化形式的:它让您编程要做什么)。请注意,重新基址只是一组命令,指示git在另一个提交(不一定是分支)上应用(或挤压,或删除等)一系列补丁。在您的特定情况下,您甚至可以通过使用git reset和git cherry pick来执行所有相同的操作。一般来说,重定基址就是在基本提交上添加补丁。谢谢你的帮助:)我今天尝试了第二种方法。。。完全按照您的步骤,我在运行git rebase时只有一个选择-i mainbranch(尽管在运行git rebase时我有两个选择-i branch1)不确定为什么我在第一个rebase时只有一个选择-由于不确定性,我再次采用了您的第一种方法-有什么想法吗?非常感谢