15

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.

btse
  • 7,811
  • 3
  • 25
  • 30
  • If you're not changing that file yourself, then just copy it and commit. – Assaf Lavie Apr 26 '14 at 18:21
  • I am changing the file I want to merge with. – btse Apr 26 '14 at 18:23
  • Well, depending on how frequently it is changed at both sides it might still be simplest to just copy it and resolve any conflicts that arise. If it were a directory/library I would suggest having it as a submodule, but for one file... anyway, the right thing to do, when you can help it, is for everyone who collaborates on a file to work on forks of the same repository. There's no way to just grab the detached history of one file in Git in order to merge it to a completely different repository AFAIK. – Assaf Lavie Apr 26 '14 at 18:56
  • Kdiff3 or other similar merging applications help me out a lot with merging different repositories with nearly the same code. – Naguib Ihab Jul 09 '15 at 04:15

4 Answers4

11

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
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
tmarwen
  • 15,750
  • 5
  • 43
  • 62
6

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`
jthill
  • 55,082
  • 5
  • 77
  • 137
1

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
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Ghanshyam Nakiya
  • 1,602
  • 17
  • 24
0

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

khano
  • 1