Git: Creating Git Submodules out of existing repos

2019-05-09 19:10发布

Please note that I am new to Git.

I have two repos:

  1. Libgdx REPO
  2. gdx-sqlite REPO

The main repository is Libgdx REPO and all of my work goes into this repository, mainly in the gdx-sqlite project. Since I wanted gdx-sqlite project to appear as a separate repository, what I did was:

  1. Created a new repo on GitHub (gdx-sqlite REPO)
  2. Created a local repo (nested inside the local libgdx repo) on my machine
  3. Pushed all the code of the local nested repo to remote gdx-sqlite REPO

This resulted in everything going wrong and I assumed that nested repository was the main culprit. Later I deleted the local nested repository and reverted to a previous commit. I have found out that a solution to this kind of problem is Git Submodules but I am completely lost at what I am trying to achieve which is as follows.

Now how am I supposed to achieve this in light of the following:

  1. I want to create a new repo located at "gdx-sqlite REPO" that should always reflect the changes I make to the project located at "https://github.com/mrafayaleem/libgdx/tree/master/extensions/gdx-sqlite"
  2. Anyone who sends a Pull Request to "gdx-sqlite REPO" should be able to pull all the dependencies (that is the complete libgdx REPO) so that he can work on that extension right away.

How can this be made possbile?

3条回答
2楼-- · 2019-05-09 19:26

You're right. The good approach for this kind of situation is to use Git submodules. This is the best way to handle dependencies in Git.

As gdx-sqlite is an extension of your main project Libgdx, you could do it this way to add it as a submodule:

git clone git@github.com:mrafayaleem/libgdx.git libgdx
cd libgdx
# remove the old directory
rm -rf extensions/gdx-sqlite
git add extensions/gdx-sqlite
# add the submodule (note the read-only URL)
git submodule add git://github.com:mrafayaleem/gdx-sqlite.git extensions/gdx-sqlite
# commit the changes
git commit -m 'Add submodule for the gdx-sqlite extension'

Then to clone your project (and all its submodules):

git clone --recursive git://github.com/mrafayaleem/libgdx.git

Or for you (with write access):

git clone --recursive git@github.com:mrafayaleem/libgdx.git
查看更多
可以哭但决不认输i
3楼-- · 2019-05-09 19:32

You should take a look at git subtree. This allows you to add one or more git repository as subtrees.

One of the drawbacks is, that you need to be carifully with your commits. If you commit several changes with files in different sub trees, than all the repositories will have this commit in its history. This can be really nasty if you try to pin down a change in one repo and can not find the file mentioned in the log because the file never belonged to this repo. A solution is to split commits to the different repositories.

查看更多
smile是对你的礼貌
4楼-- · 2019-05-09 19:32

This worked for me:

git ls-files --stage | grep $160000

Based on this great article: http://www.speirs.org/blog/2009/5/11/understanding-git-submodules.html

查看更多
登录 后发表回答