Github:恢复后忽略的更改(git cherrypick,git rebase)

Wen*_*Adi 4 git github

假设我有两个分支AB

(A)--------(A+B)--------------(A+B+R)
 \        / merge   \ revert  /
  \      /           \       /
 (B)----(B+)         (R)----
Run Code Online (Sandbox Code Playgroud)

首先,我将分支合并BA,然后我使用 GitHub 的恢复功能恢复了合并请求。

现在,当我在分支上修复一些代码B并需要A再次合并时,几乎所有更改(我修复的新更改除外)都将被忽略。我怎样才能再次获得更改?

Dav*_*sch 8

您需要还原还原,即还原您之前还原创建的提交。

这样做的原因是合并实际上做了两件事:它更改文件内容以反映合并,并且还创建一个具有 2 个父项的提交来告诉 git 合并了什么。当您还原时,它会撤消第一件事,但不会撤消第二件事。因此,当您尝试重新进行合并时,git 不知道您恢复了先前的合并,因此它会忽略之前的所有内容。


Cod*_*ard 1

请阅读此处如何“重置”您的更改 如何将 HEAD 移回之前的位置?(分离头)

一旦您获得了所需的代码,它就会再次将其推送到存储库。由于您执行了恢复而不是重置,因此您可以简单地推送代码而不会出现任何问题。

如果你已经完成了 areset并且你希望更新远程分支,你将不得不force推送git push -f origin master,这将导致变基,这也会影响你所有的同事。


我怎样才能再次获得更改?

git cherry-pick

最简单的方法就是git cherry-pick再次选择所需的提交回您的分支。

# Find out the range of commits you wish to re-add to your branch.
# then use cherry-pick to add them back to the branch

git cherry-pick start..end

# If you wish to include the start commit as well add the ^
# This will result in a cherry-pick of the start commit included as well 
git cherry-pick start^..end
Run Code Online (Sandbox Code Playgroud)

git rebase --onto(小心=变基)

# reset it to the start commit
git reset --hard start

# rebase every commit after b and transplant it onto a
git rebase --onto commit1 commit2 commit3 ... commitN
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述