开发者

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 abl开发者_如何学运维e 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.


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.


When you make changes to a particular directory, stage those changes and commit and push, you are effectively "pushing" just that directory.

If the other Git repo has only the directory you want to push, and that is why you want to have it as a repo in itself, you can look at Git Submodules.


Have you looked into git submodules? It allows git repositories to cleanly nest, but remain fully independent.

This means you can have a subdirectory with a different remote url for push/pull and different commit tree+log without cluttering up the main project.

https://git.wiki.kernel.org/index.php/GitSubmoduleTutorial

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜