Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: include part of a project into another project

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?

like image 870
shootnix Avatar asked Sep 20 '13 09:09

shootnix


People also ask

Can I have a Git project inside a Git project?

Submodules allow you to keep a Git repository as a subdirectory of another Git repository. This lets you clone another repository into your project and keep your commits separate.

How do I put two projects in one Git repository?

Solution 2 Create two independent repositories, and push them to the same remote. Just use different branch names for each repo. Thanks a lot, that should be able to solve my problem. Actually, the two solutions use branchs to hold different projects.

Can a repository have multiple projects?

Yes. You can put multiple projects in one Git repository but they would need to be on different branches within that repo.

How do I import code from one Git repo to another?

Go to the repo where you want the other repo to be merged, and run the script. Now push the changes on the master branch to remote/origin.


3 Answers

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

like image 88
DerekC Avatar answered Oct 11 '22 22:10

DerekC


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).

like image 32
Jordan Lev Avatar answered Oct 11 '22 21:10

Jordan Lev


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'
like image 29
Bvoid Avatar answered Oct 11 '22 21:10

Bvoid