75

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!

5
  • 2
    it's hard for me to tell if that is exactly what i'm looking for... seems a bit complicated. Commented Apr 28, 2012 at 7:18
  • @masterkrang You do want submodules. :) They are a little complicated at first, like git itself, but the complexity is worth it. Commented Apr 28, 2012 at 7: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. Commented Apr 28, 2012 at 8: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. Commented Apr 28, 2012 at 23:34
  • 1
    git-scm.com/book/en/v2/Git-Tools-Submodules Commented Nov 19, 2022 at 3:42

2 Answers 2

59

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").

Sign up to request clarification or add additional context in comments.

8 Comments

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!
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
+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.
@ArupRakshit as a training, I recommend training.github.com, gitimmersion.com and pcottle.github.io/learnGitBranching
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.
|
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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.