Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Projects With Common Code

Tags:

git

We have several projects in the works that share a majority of their code/config files. The framework we're using has certain directory and file dependencies that limit us to how much we can segregate common code. For example, between 'common', 'projectA', and 'projectB' we might have:

/projectA

  • /shared_dir1
    • common1
    • fileA1
    • fileA2
  • /common_dir2
    • common2
  • /dir3
    • fileA3
  • common3
  • fileA4

/projectB

  • /shared_dir1
    • common1
    • fileB1
  • /common_dir2
    • common2
  • /dir3
    • fileB2
    • fileB3
  • common3
  • fileB4
  • fileB5

We currently manage this with 3 Git projects: 'common', 'projectA', and 'projectB', with common files separated out into 'common' and project specific files in their own projects. 'projectA' and 'projectB' have a .gitignore with entries for all common dirs and all common files under each shared dir. A script copies 'common' into the project you want to work in and development is done there. Once a change is done, another script copies all common dirs and common files back into 'common'. Changes to 'common' and to the project can then be seen via 'git status'.

This obviously comes with its headaches of copying back and forth between 'common', keeping .gitignore accurate, and switching branches within 'projectA' and 'projectB'. As we plan for 'projectC-F', though, this approach seems nice as we can avoid merging common changes to N projects.

Looking for advice on how to better maintain this type of structure. Submodules seem undoable given the lack of segregation, unless we did a large number of them. I've seen some promising alternatives using symlinks, but that also comes with it's issues. Any advice would be appreciated.

like image 566
Steve Avatar asked Apr 22 '11 01:04

Steve


People also ask

How do I share code between git repositories?

By using git submodules , you can have an independent repository inside a parent repository. So you have your web and mobile repository and then inside it, in a folder, the shared code repository. If you cd into the shared code folder, you can pull, commit, make branches and push back to the shared code.

Can I add multiple projects in one repository?

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


2 Answers

Have you considered git subtree?

Something like this can be done:

git remote add common git://server/common.git
git fetch common
git checkout -b common_branch common/master
git checkout master
git read-tree --prefix=common/ -u common_branch

Read more in the links below:

http://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html

http://progit.org/book/ch6-7.html

like image 102
manojlds Avatar answered Oct 06 '22 17:10

manojlds


If you're willing to put the shared code in a separate git repository, you might be interested in git submodules. A submodule allows you to include a git repo within another repo.

Here are a couple links:

  • http://progit.org/book/ch6-6.html
like image 39
Andy White Avatar answered Oct 06 '22 15:10

Andy White