8

I have a quite difficult situation: in one project (git repo) I have a file, and I need it in another project (another git repo). Is there any clear and (as it possible) simple way to put it into my project? It's looks like:

git-repo-1:
    lib/foo
    lib/ololo

git-repo-2:
    lib/*foo* (from repo-1)
    lib/bar
    lib/baz 

My question is: 1. How can I do it? 2. How can I update the file (inc. from git-repo-1) into my repo (git-repo-2)?

p.s. Yes, a saw many pages about subtrees, but... it still not so clear for me. Any suggestions? Even instruction ;-) I'll be so appreciate for anything.

UPD: What I want: http://git-scm.com/book/en/Git-Tools-Subtree-Merging BUT! I want to get only a part of "rack" repository into my project repo.

The question is: how to split off the file I need from one repo and merge it to another?

shootnix
  • 305
  • 2
  • 10
  • 1
    Try using [submodules](http://book.git-scm.com/5_submodules.html)? – F.X. Sep 20 '13 at 09:12
  • I can't understand how. Can you help me? – shootnix Sep 20 '13 at 09:16
  • possible duplicate of [How to set up a git project to use an external repo submodule?](http://stackoverflow.com/questions/2140985/how-to-set-up-a-git-project-to-use-an-external-repo-submodule) – F.X. Sep 20 '13 at 10:32
  • Just use `file:///path/to/your/git-repo-1` as the remote URL, it'll do the trick. A little Googling would probably help, too ;) – F.X. Sep 20 '13 at 10:34
  • 2
    Thanks, but this is not exactly what I want: partial including one repo into another or including part of one repo into another. – shootnix Sep 20 '13 at 11:55

4 Answers4

2

extract lib/foo into a new project called common.
then include the full common project in both project 1 and 2

DerekC
  • 31
  • 2
1

I think you have two separate questions here. The first is how to include one repo in another (which the modules or subtree features are for). The second is how to only use PART of a git repo.

I am not a git expert, but have been told by some of them that it is not really possible to only utilize portions of a repository -- because of how git works, where a lot of history and changes are stored as "deltas" (so only the portions of files that changed from one version to another are stored in a newer version... or something like that).

So... I think your best bet here is to bring down project 2 in a totally separate directory, then copy just the files you need out of there into your project 1. (Or set up some kind of symlink / alias thing so you don't have to copy them).

Jordan Lev
  • 2,773
  • 2
  • 26
  • 39
1

I fear you can't include only a part of another repository in yours. To deal with that problem, I have a small script that checks out the latest version of the file I need into the current working directory. If you use "git archive", you can retrieve the latest version from upstream :

For example :

git archive --format=tar --remote ssh://git@gitserver:7999/infra/scripts.git HEAD supplier.csv | tar xf -


git add ./supplier.csv ;


git commit -m 'Update suppliers from upstream'
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
Bvoid
  • 23
  • 4
0

I do not recommend to go deeply in git. In my opinion, your project must be as clear as possible.

You can do the job you need manually or using script:

cp path/to/git-repo-1/lib/foo path/to/git-repo-2/lib/
git add .
git commit -a -m 'updated lib/foo'
WebPal
  • 818
  • 8
  • 16