【问题标题】:How to apply changes of a specific file of the other git repository to my own repo如何将其他 git 存储库的特定文件的更改应用于我自己的存储库
【发布时间】:2017-03-19 13:53:03
【问题描述】:

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

附:如果可能的话,我想在提交这个更改到 repo b 时保留原作者。

【问题讨论】:

    标签: git version-control diff patch


    【解决方案1】:

    您可以使用以下步骤将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
    

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

    【讨论】:

    • 谢谢。这应该可以,但是对其他一些文件(我不想提交)的修改也将提交给我的 repoB。我应该使用git cherry-pick xxx -n 并接受/拒绝文件的更改并手动提交。
    【解决方案2】:

    您需要在 repo-b 中添加一个带有 repo-a-url(比如 repob)的新遥控器。然后 checkout 将该文件 repo-b 任何特定的分支(比如 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
    

    【讨论】:

    • 谢谢。这个也应该工作,但如果我修改文件的另一部分,它就不起作用(我的错不用提)。 Cherry-pick 应该比这做得更好。
    猜你喜欢
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 2017-03-09
    • 2013-03-03
    • 1970-01-01
    相关资源
    最近更新 更多