Git merge single file from another repository into my own

巧了我就是萌 提交于 2019-12-18 15:19:19

问题


Say I have my own git repo with a bunch of text files in it. There is another different git repo that someone else owns with a bunch of text files that all differ from my own except for one file.

I am continuously making changes to the different text files in my repo, but every now and then I want to merge any changes of that single file from the other repo into my own.

Is there any easy way of going about doing this? I've searched around and found some similar questions but none that we're for my exact scenario.


回答1:


Try to add the other-repo then bring its branches including your target-branch

~ $ git remote add other git@github.com:username/other-repo.git
~ $ git fetch other

Switch to the branch you want to merge in the target-file.ext:

~ $ git checkout your-branch

Merge the target file into yours:

~ $ git checkout -p other/target-branch target-file.ext



回答2:


You can merge arbitrary files or repo objects. Simplest might be:

git merge-file file.txt <(git show last-merged-file.txt) /path/to/their/file.txt
# resolve any conflicts, then
git tag last-merged-file.txt `git hash-object-w file.txt`



回答3:


If you are using any kind of packaging (artifacts) results in the original repo, then you can ask the owner of the repo to publish the sources as well. Then in your own repo you consume and unpack such artifact and just copy the file you need. E.g. If using Java-Maven, make sure the build on the original repo publishes also the sources, then in your own repo, you put a pre-step (before compile) in which you download the sources dependency, unpack the jar file and copy the one you need to your proper directory. There are maven plugins for that




回答4:


Go to your current directory, This way I'm doing currently, I hope you will help too.

cd your_current_git_Directory

Set your username and repository of another repository/second(from want to copy)

git remote add -f repo-b git@github.com:<username>/<repository>.git

Now, Merge the filename you looking for(if you want file merge subfolder)

 git checkout -p repo-b/master  <folder_name>/<filename>

OR merge filename only

 git checkout -p repo-b/master  <filename>

and Remove repository

 git remote rm repo-b


来源:https://stackoverflow.com/questions/23314805/git-merge-single-file-from-another-repository-into-my-own

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!