Git:你如何将一个文件与之前提交的一个版本进行合并? [英] Git: How do you merge a file with a version of itself from a previous commit?

查看:1069
本文介绍了Git:你如何将一个文件与之前提交的一个版本进行合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在提交A中对文件进行了一些更改,然后我错误地解开了更改,并继续进行提交B和C中的更改。

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.

我希望提交A中的更改位于我的文件中,但B和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.

假设我的分支现在处于

我不想要

I don't want to

$ git checkout --patch 

因为我希望该文件包含我在B和C中所做的更改并执行从提交A中检出文件将重写索引和工作树中的文件。

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.

我无法做一个樱桃选择,因为提交A是一个合并提交(有两个贡献者存储库,我删除了我的导师在我将它们合并后在后续提交中错误所做的更改),如果我指定了其中一个父项,则最终可能会导致混乱。

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?

推荐答案

您可以创建补丁并尝试应用修补程序。如果遇到冲突问题,如

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

合并需要人工干预。

下面是一个例子:

Here's an example:

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

这种策略有可能仍然存在由于提交A的性质而产生问题。您无法分离合并提交的组成部分,而无需访问从中进行单独提交的分支。你的分支/仓库只是将其视为一个提交。

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.

这篇关于Git:你如何将一个文件与之前提交的一个版本进行合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆