git merge:将更改应用于移至其他文件的代码 [英] git merge: apply changes to code that moved to a different file
问题描述
git合并my_branch his_branch
时,git没有注意到新文件中的代码与旧的代码相同,所以我的更改都不存在。 将我的更改再次应用于新文件中的代码最简单的方法是什么?我不会有太多问题发现需要重新应用哪些提交(我可以使用 git log --stat
)。但据我所知,没有办法让git将更改重新应用到新文件中。我现在看到的最简单的事情是手动重新应用更改,这看起来不是一个好主意。
我知道git可以识别blob,而不是文件,所以肯定有一种方法可以告诉它,应用此提交中的确切代码更改,但是现在它在这个新文件中。。
我有类似的问题,我通过重新绑定我的努力匹配目标文件组织。假设你在分支上修改了 original.txt
( local $> c $ c> branch),但是在master分支上,
original.txt
已被复制到另一个,比如 copy.txt
。
这个副本已经在一个提交中完成了,我们提交了 CP
。
你想申请所有本地更改,在 original.txt中提交
和 B
到新文件 copy.txt
。
---- X ----- CP ------(master)
\
\ - A --- B ---(local)
在出发点创建一次性分支 move
你的改变。
也就是说,将 move
分支放在提交 X
处,合并;最有可能的是,这是您为实现您的更改而从中提出的承诺。如下面的用户 @digory doo 所示,您可以执行 git merge-base master local
来查找 X
$ p $ ---- X(移动)----- CP -----(主)
\
\ - A --- B ---(本地)
在此分支上,发出以下重命名命令:
git mv original.txt copy.txt
这会重命名文件。请注意,此时您的树中尚不存在 copy.txt
。
提交您的更改(我们将该提交命名为 MV
)。
/ - MV(移动)
/
---- X ----- CP -----(主)
\
\ - A --- B ---(本地)
您现在可以在 move
之上重新分配您的工作:
git rebase move local
这应该没有问题,并且您的更改应用于您本地分支中的 copy.txt
。
/ - MV(移动)--- A'--- B'---(本地)
/
---- X - --- CP -----(master)
现在,您不一定需要或需要在主分支的历史记录中提交 MV
,因为移动操作可能导致与提交 CP $ c $时的复制操作发生冲突c>在主分支中。
您只需要
git rebase move local --onto c
...其中 CP
是其中 copy.txt
是在其他分支中引入的。
这将重新设置 copy.txt
上 CP
提交之上的所有更改。
现在,您的本地
分支就像您总是修改 copy.txt
而不是 original.txt
,您可以继续与其他人合并。
/ - A '' - B'' - (本地)
/
----- X ------- CP -----(主)
重要的是,这些更改应用于 CP
或其他 copy.txt
将不存在,并且这些更改将应用于 original.txt
。
希望这很清楚。
这个答案来得晚,但这可能对其他人有用。
I am attempting a pretty beefy git merge maneuver right now. One problem that I am coming across is that I made some changes to some code in my branch, but my colleague moved that code to a new file in his branch. So when I did git merge my_branch his_branch
, git did not notice that the code in the new file was the same as the old, and so none of my changes are there.
What's the easiest way to go about applying my changes again to the code in the new files. I won't have too many problems finding out which commits need to be reapplied (I can just use git log --stat
). But as far as I can tell, there is no way to get git to reapply the changes into the new files. The easiest thing I am seeing right now is to manually reapply the changes, which is not looking like a good idea.
I know that git recognizes blobs, not files, so surely there must be a way to tell it, "apply this exact code change from this commit, except not where it was but where it now is in this new file".
I had a similar issue, and I resolved it by rebasing my work to match the target file organization.
Say that you modified original.txt
on your branch (the local
branch), but on the master branch, original.txt
has been copied to another one, say copy.txt
.
This copy has been done in a commit that we name commit CP
.
You want to apply all your local changes, commits A
and B
below, that were made on original.txt
, to the new file copy.txt
.
---- X -----CP------ (master)
\
\--A---B--- (local)
Create a throwaway branch move
at the starting point of your changes.
That is to say, put the move
branch at commit X
, the one before the commits you want to merge; most likely, this is the commit from which you branched out to implement your changes. As user @digory doo wrote below, you can do git merge-base master local
to find X
.
---- X (move)-----CP----- (master)
\
\--A---B--- (local)
On this branch, issue the following renaming command:
git mv original.txt copy.txt
This renames the file. Note that copy.txt
did not yet exist in your tree at this point.
Commit your change (we name this commit MV
).
/--MV (move)
/
---- X -----CP----- (master)
\
\--A---B--- (local)
You can now rebase your work on top of move
:
git rebase move local
This should work without problem, and your changes are applied to copy.txt
in your local branch.
/--MV (move)---A'---B'--- (local)
/
---- X -----CP----- (master)
Now, you do not necessarily want or need to have commit MV
in your main branch's history, because the move operation may lead to a conflict with the copy operation at commit CP
in the main branch.
You only have to rebase your work again, discarding the move operation, as follows:
git rebase move local --onto CP
... where CP
is the commit where copy.txt
was introduced in the other branch.
This rebases all the changes on copy.txt
on top of the CP
commit.
Now, your local
branch is exactly as if you always modified copy.txt
and not original.txt
, and you can continue merging with others.
/--A''---B''-- (local)
/
-----X-------CP----- (master)
It is important that the changes are applied on CP
or otherwise copy.txt
would not exist and the changes would be applied back on original.txt
.
Hope this is clear. This answer comes late, but this may be useful to someone else.
这篇关于git merge:将更改应用于移至其他文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!