如何将其他 git 存储库的特定文件的更改应用到我自己的存储库

How to apply changes of a specific file of the other git repository to my own repo

有两个存储库 /a/.git/b/.git,它们都有一个名为 c.txt 的文件。这个文件之前在他们之间是相同的,但现在 c.txt 在 repo a 中被修改了。如何在不手动复制粘贴的情况下提取此更改并将其应用于 repo bc.txt 的修改与 repo a 中的其他更改一起提交,但我只想要特定 c.txt.

的补丁

P.S。如果可能的话,我想在将此更改提交到 repo b.

时保留原作者

您需要在 repo-b 中添加一个带有 repo-a-url(比如 repo)的新遥控器。然后 checkout 将文件回购到任何特定分支(比如,master)。

进入 repo-b 然后尝试以下命令:

$ git remote add repoa <repo-a-url>     # add a new remote repoa = repo-a-url
$ git fetch repoa                       # sync with repoa

$ git checkout repoa/master c.txt       # checkout the file to 'repoa' 'master' branch

$ git log --numstat --oneline c.txt     # show the change lists of c.txt file

您可以使用以下步骤使 c.txt 的版本从 repoA (/a/.git) 到 repoB (/b/.git):

# In local repoB /b/.git
git remote add -f a /a/.git
gitk --all   #To find the commit id where remotes/a/master point to (assume the new version of c.txt in master branch)
git cherry-pick <commit id you find in above step> -X theirs

现在 c.txt 的新版本 /b/.git 保留原作者。