73

Am I going to run into any issues if I git clone a repo into an existing git repo?

For sake of simplification, I am developing a library "lib/" that should be available to all of my projects. This is a separate git repo. I'd like to import this lib/ into all of my projects, and update it only in one place, never touch it from any of the projects, just use it.

I am assuming this is ok, just wondered if there is anything I should watch out for. Thanks!

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
botbot
  • 7,299
  • 14
  • 58
  • 96
  • 2
    it's hard for me to tell if that is exactly what i'm looking for... seems a bit complicated. – botbot Apr 28 '12 at 07:18
  • @masterkrang You do want submodules. :) They are a little complicated at first, like git itself, but the complexity is worth it. – Asherah Apr 28 '12 at 07:53
  • haha. alright, i believe it now, i *do* need submodules. thanks a lot, Maxpm and @Arlen Cuss. i will try to tackle that beast tomorrow when i'm full of energy. – botbot Apr 28 '12 at 08:04
  • one thing about that submodules link above that i can't really appreciate is all the mumbo jumbo with creating a for loop to create the submodules... if you've never seen submodules before, that's a little bit annoying. i think it would have been more instructive and less confusing if he simply created a single submodule in a more real-life scenario. i found and posted some links below that do exactly that. – botbot Apr 28 '12 at 23:34
  • https://git-scm.com/book/en/v2/Git-Tools-Submodules – Anthony Rutledge Nov 19 '22 at 03:42

2 Answers2

56

Just for the record, you can clone a git repo within another one:
Everything under your lib directory will be ignored by the enclosing Git repo, because said lib directory contains a .git.

So it would work, but the enclosing repo would have no idea:

  • it needs a lib directory from another repo
  • it needs a specific revision of that lib to build properly, even though it would record the SHA1 of the nested lib repo tree. (that is a gitlink, a special entry in the index of the parent repo)
    That means cloning the enclosing repo, and you will get an empty "lib/" folder.

Those (repo URL and repo SHA1) are precisely the two informations recorded by the parent repo (the enclosing one) in order to reference a submodule.
It is made to give you access to a fixed revision of another repo within your repo, but as explained in "True nature of submodules", that doesn't prevent you to locally modify lib directly within your parent repo.
(As long as you commit your modifications in lib first, then go one level up back in your parent repo, and commit there as well)

The main benefit to any contributor of your main project is that, when they will clone said project, they will know they also need lib if it is declared as a submodule (as mentioned in "Git Submodule Workflow Advice").

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • that makes a lot of sense @VonC. thanks for the information. i kinda figured you could clone into an existing git repo, but wasn't sure how git would treat it. ignoring it seems elegant and logical. running a test in an existing repo didn't seem safe. testing out a scaled down version just didn't seem real-world enough, so i figured i'd look to the community. last thing i want is an unweildy repo inside a repo when s*** hits the fan. thanks again! – botbot Apr 28 '12 at 21:57
  • 3
    this seems like it would be a great place to post a super simple example (or link to example) of how to init a git repo and how to clone or add the sumodule – botbot Apr 28 '12 at 22:11
  • 4
    +1 @VonC for actually answering the question, and explaining the subtlety between cloning into a repo and using a submodule, which I would bet is the question many people are asking. – Mark Mikofski Sep 16 '12 at 21:41
  • @ArupRakshit all the good resources are listed at http://stackoverflow.com/tags/git/info – VonC May 17 '14 at 16:49
  • 1
    @ArupRakshit as a training, I recommend http://training.github.com/, http://gitimmersion.com/ and http://pcottle.github.io/learnGitBranching/ – VonC May 17 '14 at 16:51
  • @ArupRakshit around late 2008 (time of my first answers about Git on Stack Overflow: http://stackoverflow.com/a/255212/6309) – VonC May 17 '14 at 16:54
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/53884/discussion-between-arup-rakshit-and-vonc) – Arup Rakshit May 17 '14 at 16:55
  • 1
    This is a great answer. I came across this situation, but before cloning a repo within another repo, I read the submodule link in the answer and realized that submodule is a cleaner way to achieve this. – Amey May 27 '20 at 08:32
16

I'm finding that following this tutorial is helpful for understanding submodules if you don't have much prior experience.

http://help.github.com/submodules/

https://chrisjean.com/git-submodules-adding-using-removing-and-updating/

Ahmed Mohamedeen
  • 328
  • 3
  • 11
botbot
  • 7,299
  • 14
  • 58
  • 96