Git: How do you merge a file with a version of itself from a previous commit?

我是研究僧i 提交于 2019-12-13 03:44:28

问题


There are some changes I made to a file in a commit A, then I undid the changes by mistake and went on further to make changes in commits B and C.

I want the changes in commit A to be in my file, but the changes in B and C should not be lost either.

Let's say my branch is now at C.

I don't want to

$ git checkout --patch 

because I want the file to contain the changes I made in B and C and doing a checkout of the file from commit A will rewrite the file in both the index and the working tree.

I can't do a cherry-pick because commit A is a merge commit(there are two contributors to the repository and I deleted the changes my mentor made by mistake in the subsequent commits after I merged them) and I might end up with a mess if I specified either parent.

Apart from manually copying the changes I want in the file, is there any other way to accomplish this?


回答1:


You can create a patch and attempt to apply the patch. If you experience issues with conflicts such as

error: patch failed: <file_name>:1
error: <file_name>: patch does not apply

the merge will require manual intervention.

Here's an example:

$ git format-patch -n <sha_from_commit_you_want_to_merge> > patch_file.patch
$ git apply patch_file.patch

It is possible this strategy will still be problematic because of the nature of commit A. You cannot separate constituent components of a merge commit without access to the branch from whence the individual commits were made. Your branch/repo just sees that as one commit.

Of course if you do have access to the original branch you can just create a patch file from there, even if they are in different repositories.



来源:https://stackoverflow.com/questions/17582874/git-how-do-you-merge-a-file-with-a-version-of-itself-from-a-previous-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!