在两个不同的 git repo 中获取文件之间的差异并应用于第一个 repo [英] get diff between files in two different git repos and apply to first repo
问题描述
我有两个不同的本地 git 存储库,我们将其称为 a
和 b
.它们都在我的主目录中的 projects
文件夹中.我想在两个存储库中的特定文件之间获得差异,我们称之为 foo
,并将该差异应用于 a
.
I have two different local git repos, which we'll call a
and b
. They are both in a projects
folder in my home directory. I want to get a diff between a particular file in both repos, let's call it foo
, and apply that diff to a
.
如果我运行 git diff ./foo ../b/foo >diff.diff
来自 a
我可以查看 diff 并确认它包含我想要应用的更改,但是当我运行 git apply diff.diff
然后我得到这个错误:
if I run git diff ./foo ../b/foo > diff.diff
from inside a
I can look at the diff and confirm that it contains the changes I want to apply, but when I run git apply diff.diff
then I get this error:
fatal: invalid path './foo'
如果我尝试运行 git diff ~/projects/a/foo ~/projects/b/foo >diff.diff
,然后当我尝试应用 diff 时出现此错误:
If I try running git diff ~/projects/a/foo ~/projects/b/foo > diff.diff
, then when I try to apply the diff I get this error:
error: home/ryan/projects/a/foo: No such file or directory
现在,我可以只复制文件以获得与应用差异相同的效果,但是有充分的理由为什么这不起作用?
Now, I can just copy the file to get the same effect as applying the diff, but is there a good reason why this isn't working?
推荐答案
比较 repo a
和 repo b
之间的 foo
文件并应用对 repo a
的更改,您只需要使用以下命令:
To compare foo
file between repo a
and repo b
and apply the changes to repo a
, you just need to use below commands:
# In ~/projects/a (local repo a)
git remote add repob ~/projects/b -f
git diff master repob/master -- foo > diff.patch
git apply diff.patch
现在 repo a
中的 foo
更改为 repo b
的版本.然后你可以提交和推送:
Now the foo
in repo a
change to the version of repo b
. Then you can commit and push:
git add foo
git commit -m 'change foo as the version in repo b'
git push
这篇关于在两个不同的 git repo 中获取文件之间的差异并应用于第一个 repo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!