26

I've a Xcode project which itself has Git Source Control. In a Libraries folder I've cloned eight other Git project from GitHub. They are located inside my own Git repository and I've added all these libraries to my git in a commit.

Instead of having the code of all these git libraries in my repository, is there a way to let git download their code from their repo when I make a clone of my repo? Or is it normal to include other git repos inside a project?

dhrm
  • 14,335
  • 34
  • 117
  • 183

2 Answers2

29

Sure do the following:

  1. Remove the 3rd-party-folder which you might have added already
  2. Open your Terminal and execute the following commands

    cd /path/to/your/main/repo
    git submodule add git@github.com:someuser/somerepo.git somerepo
    git commit -m "Added submodules"
    
  3. Now instead of copying those files you'll have a reference to the other repository in your project:

Edit:

Now if you want to update the submodule to a more recent commit you can do the following:

cd somerepo
git pull # You can also checkout a branch / tag etc.
cd ..
git add somerepo
git commit -m "Telling the main repo to update the reference to the referenced repo"
Besi
  • 22,579
  • 24
  • 131
  • 223
  • Thank you. Couldn't I use this instead `git submodule add https://github.com/AlanQuatermain/AQGridView.git Libraries/AQGridView` instead of `git@github.com:...`? – dhrm Jan 17 '12 at 15:30
  • How can I update these submodules? Are they pulled automatically when I pull my repo or do I have to go inside their folder and make a `git pull`? – dhrm Jan 17 '12 at 15:32
  • @DennisMadsen Yes you can do a git pull which does pull any changes in `somerepo` however, you have to tell your main repository to update the reference too. See my updated post. BTW: I find submodules quite handy since you don't have to duplicate code from repositories, that are already accessible for you via git. – Besi Jan 17 '12 at 15:40
  • @DennisMadsen: You can use any repository path that Git will accept; SSH, HTTP(S), Local path, etc. – Justin ᚅᚔᚈᚄᚒᚔ Jan 17 '12 at 15:42
  • For anyone who's interested. The screenshot were done with [Git Tower](http://www.git-tower.com/), which I can recommend using. – Besi Jan 18 '12 at 07:46
2

You can use git submodule and clone repositories like this

Community
  • 1
  • 1
gustavotkg
  • 4,099
  • 1
  • 19
  • 29