重新应用通过 Github 合并和撤消的更改
Re-applying changes that were merged and undo'd through Github
我不小心通过 Github PR 将一个分支合并到 master
。我立即将其还原(也通过 github)。几天后,当该代码经过测试并准备好合并时,合并和 PR 实际上不会将代码放入 master,因为提交已经存在于 github 中(但代码不存在,因为它被还原了。
因此,在进行了数周不相关的提交之后,现在我正在尝试将这些更改恢复为 master。这大致是 git log --oneline
输出的内容,并对相关点进行了一些评论:
54c73ee (HEAD, origin/master, origin/HEAD, staging) Merge pull request #637 from leonsas/last-night-view-metric-ceiling
...
More unrelated changes that should stay in.
...
af602f0 Merge pull request #639 from leonsas/staging
67ded36 (origin/staging) Merge <-- Here's where I tried merging the changes again, but it doesn't make it into the codebase.
...
Bunch of unrelated commits that should stay.
...
a603d0b Merge pull request #633 from leonsas/revert-628-hr-hrv-audit
c3da670 (origin/revert-628-hr-hrv-audit) Revert "Hr hrv audit"
01f2fab Merge pull request #632 from leonsas/revert-629-always-get-hrvz
5824fc8 (origin/revert-629-always-get-hrvz) Revert "Always get hrvz" <-- I reverted changes, because code wasn't tested
b75a537 First iteration at setting the max value on the chart
6939035 Merge pull request #631 from leonsas/is-valid-fix
87b53d5 Merge pull request #629 from leonsas/always-get-hrvz <-- These changes I want in
5b9a848 Merge pull request #628 from leonsas/hr-hrv-audit
将这些更改重新应用回 master 的最佳方法是什么?
-- 更新一
根据 Thomas 的建议,我再次尝试了 rebase 解决方案:
> git checkout -b hrv-almost-latest-changes e1d0d7b
> git rebase master-clone
First, rewinding head to replay your work on top of it...
Fast-forwarded hrv-almost-latest-changes to master-clone.
> git push --set-upstream origin hrv-almost-latest-changes
但是 master
与 hrv-almost-latest-changes
完全同步,所以没有什么可以在 github PR 上合并。
-- 更新 2
总的来说,樱桃采摘效果很好。具体解决方案是:
git checkout master
git checkout -b hrv-merge-fix
git cherry-pick -m 1 87b53d5
(solve conflicts)
git add <files from solved conflicts>
git cherry-pick --continue
git cherry-pick -m 1 5b9a848
git push origin hrv-merge-fix
问题是 PR 包含已经是当前主控的祖先的提交:
A- ... -F--G--H- ... -I
\ / |
B--C--D--E revert PR |
| current master
| merge PR
old master |
original PR
因为 B-E 是 I 的祖先,将 E 合并到 I 的尝试不会改变任何东西。
但是,您可以将 PR 分支变基到当前主分支上,以创建一个新的提交字符串,它不是 I 的祖先,并且仍然保留这些更改:
A- ... -F--G--H- ... -I
\ / \
B--C--D--E B'--C'--D'--E'
| |
master new PR branch
现在,将 E' 合并到 I 中将应用更改。
基本上,这样做:
$ git fetch
$ git checkout E
$ git checkout -b new-pr-branch
$ git rebase master
$ git push --set-upstream origin new-pr-branch
然后从 new-pr-branch
.
提交新的 PR
我不太熟悉 github 的作用 "behind the scenes",所以我会在您的本地存储库中使用命令行继续此操作。
您应该可以使用 git checkout master ; git cherry-pick -m X 87b53d5
。 X
的取值参考git help cherry-pick
:
-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the
mainline. This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to
replay the change relative to the specified parent.
我不小心通过 Github PR 将一个分支合并到 master
。我立即将其还原(也通过 github)。几天后,当该代码经过测试并准备好合并时,合并和 PR 实际上不会将代码放入 master,因为提交已经存在于 github 中(但代码不存在,因为它被还原了。
因此,在进行了数周不相关的提交之后,现在我正在尝试将这些更改恢复为 master。这大致是 git log --oneline
输出的内容,并对相关点进行了一些评论:
54c73ee (HEAD, origin/master, origin/HEAD, staging) Merge pull request #637 from leonsas/last-night-view-metric-ceiling
...
More unrelated changes that should stay in.
...
af602f0 Merge pull request #639 from leonsas/staging
67ded36 (origin/staging) Merge <-- Here's where I tried merging the changes again, but it doesn't make it into the codebase.
...
Bunch of unrelated commits that should stay.
...
a603d0b Merge pull request #633 from leonsas/revert-628-hr-hrv-audit
c3da670 (origin/revert-628-hr-hrv-audit) Revert "Hr hrv audit"
01f2fab Merge pull request #632 from leonsas/revert-629-always-get-hrvz
5824fc8 (origin/revert-629-always-get-hrvz) Revert "Always get hrvz" <-- I reverted changes, because code wasn't tested
b75a537 First iteration at setting the max value on the chart
6939035 Merge pull request #631 from leonsas/is-valid-fix
87b53d5 Merge pull request #629 from leonsas/always-get-hrvz <-- These changes I want in
5b9a848 Merge pull request #628 from leonsas/hr-hrv-audit
将这些更改重新应用回 master 的最佳方法是什么?
-- 更新一
根据 Thomas 的建议,我再次尝试了 rebase 解决方案:
> git checkout -b hrv-almost-latest-changes e1d0d7b
> git rebase master-clone
First, rewinding head to replay your work on top of it...
Fast-forwarded hrv-almost-latest-changes to master-clone.
> git push --set-upstream origin hrv-almost-latest-changes
但是 master
与 hrv-almost-latest-changes
完全同步,所以没有什么可以在 github PR 上合并。
-- 更新 2
总的来说,樱桃采摘效果很好。具体解决方案是:
git checkout master
git checkout -b hrv-merge-fix
git cherry-pick -m 1 87b53d5
(solve conflicts)
git add <files from solved conflicts>
git cherry-pick --continue
git cherry-pick -m 1 5b9a848
git push origin hrv-merge-fix
问题是 PR 包含已经是当前主控的祖先的提交:
A- ... -F--G--H- ... -I
\ / |
B--C--D--E revert PR |
| current master
| merge PR
old master |
original PR
因为 B-E 是 I 的祖先,将 E 合并到 I 的尝试不会改变任何东西。
但是,您可以将 PR 分支变基到当前主分支上,以创建一个新的提交字符串,它不是 I 的祖先,并且仍然保留这些更改:
A- ... -F--G--H- ... -I
\ / \
B--C--D--E B'--C'--D'--E'
| |
master new PR branch
现在,将 E' 合并到 I 中将应用更改。
基本上,这样做:
$ git fetch
$ git checkout E
$ git checkout -b new-pr-branch
$ git rebase master
$ git push --set-upstream origin new-pr-branch
然后从 new-pr-branch
.
我不太熟悉 github 的作用 "behind the scenes",所以我会在您的本地存储库中使用命令行继续此操作。
您应该可以使用 git checkout master ; git cherry-pick -m X 87b53d5
。 X
的取值参考git help cherry-pick
:
-m parent-number, --mainline parent-number
Usually you cannot cherry-pick a merge because you do not know which side of the merge should be considered the
mainline. This option specifies the parent number (starting from 1) of the mainline and allows cherry-pick to
replay the change relative to the specified parent.