Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a git repo inside another git repo

I have a django project managed with git. I am git pushing it to my host. Now, I want to be able to push just one of the directories (inc. all sub dirs) to another git repo. How is it possible, if it is possible at all?

Edit: So I want that directory to be a git repo itself.

like image 939
yasar Avatar asked Aug 22 '11 16:08

yasar


People also ask

Can I merge two Git repositories?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.

Can I have multiple Git repositories in one folder?

Yes, it is possible to have two git repositories in one directory.


1 Answers

You could look at git's support for submodules. You would add the second repository as a submodule to your main project, e.g. with commands like:

git submodule add git://wherever/blah.git library-code
git commit -m "Added a new submodule called 'library-code'"

When you change into the library-code subdirectory, it's as if the parent repository doesn't exist - you can change origin to use a transport that you can push over and then push as if it were completely independent.

To specify that you want the submodule to be at a particular version, you should change into the submodule and use git checkout to switch to the right version. Then you change back up to the main repository and stage and commit that new submodule version with:

git add library-code
git commit -m 'Change the submodule version'

The tree of the main repository just stores the version that the submodule should be at, so when you push the main repository, it's not pushing any of the files in the submodule.

In order to split off this subdirectory, while preserving history, you'll need to clone your original repository and user git filter-branch to rewrite the history, as described in this answer:

  • Detach (move) subdirectory into separate Git repository

Then you can push that to a newly created repository GitHub repository, return to your original project, remove the subdirectory, and replace it with the submodule as described above.

If you're not very familiar with git concepts then this may be difficult for you - I would recommend reading up on git submodules beforehand.

like image 136
Mark Longair Avatar answered Sep 16 '22 14:09

Mark Longair