[英]How to merge parts of old commit with HEAD in git?
I tried to solve a coding problem using Method A
. 我试图使用
Method A
解决编码问题。 I committed the best working example of Method A
(which I will call "Commit 5"
). 我提交了
Method A
的最佳工作示例(我将其称为"Commit 5"
)。 I wasn't fully satisfied with Method A
. 我对
Method A
并不完全满意。 So I then continued developing the program using Method B
. 因此,我随后继续使用
Method B
开发程序。 It is now at "Commit 8"
. 现在位于
"Commit 8"
。 (All in master branch) (全部在master分支中)
I've now decided to stick with Method A
, but many other parts of the code have changed since Commit 5
. 现在,我决定坚持使用
Method A
,但是自Commit 5
以来,代码的许多其他部分都已更改。 I want to wind back all the code associated with Method B
back to Method A
, but keep everything else unchanged. 我想将与
Method B
关联的所有代码都退回到Method A
,但其他所有内容保持不变。 These differences span multiple files. 这些差异跨越多个文件。
How would I best achieve this in git? 我怎样才能最好地在git中做到这一点? Is there some kind of
"manual partial revert-merge"
workflow to do this? 是否有某种
"manual partial revert-merge"
工作流程来做到这一点?
OK this is how I did it: 好的,这就是我的方法:
I got a list of files that were different between HEAD and commit 5 as follows: 我得到了在HEAD和commit 5之间不同的文件列表,如下所示:
git diff HEAD..e4647 --name-status > /tmp/filelist
I then edited that in vim to remove any of the files I didn't want to revert, and converted it to a spaced list of files called 'filestodiff'. 然后,我在vim中对其进行了编辑,以删除所有我不想还原的文件,并将其转换为一个名为'filestodiff'的文件隔开列表。 I then made and applied a patch using that list.
然后,我使用该列表制作并应用了补丁。
git diff HEAD..e4647 -- `cat /tmp/filestodiff` > /tmp/patch
git apply /tmp/patch
There was a binary file (which cannot be patched), so I manually checked it out: 有一个二进制文件(无法修补),所以我手动将其签出:
git checkout e4647 the_binary_file
That put it close enough. 这就足够接近了。 I tried editing the patch file to remove some individual line-changes I didn't want, but it just corrupted the patch, so I did those few line changes manually.
我尝试编辑补丁文件以删除一些我不希望的单独的换行,但是它只是破坏了补丁,因此我手动进行了一些换行。
Finally, I made sure any new files were git added, it still built, then: 最后,我确保添加了所有新文件,并且仍在构建,然后:
git commit -am 'hurray! its not borked anymore!'
Assuming all the changes in Method B
are in Commit 8
, you can simply revert Commit 8
. 假设
Method B
中的所有更改都在Commit 8
,则可以简单地还原Commit 8
。
Check: 校验:
https://help.github.com/desktop/guides/contributing/reverting-a-commit/ https://help.github.com/desktop/guides/contributing/reverting-a-commit/
https://github.com/blog/2019-how-to-undo-almost-anything-with-git https://github.com/blog/2019-how-to-undo-almost-anything-with-git
运行git rebase -i Commit5
,然后您可以根据需要选择要使用的提交和编辑提交。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.