user3639557
user3639557

Reputation: 5311

how to work with git submodule in a new repository?

I have been handed a project from someone else and I am having a hard time to get the repo up and running. It might sound very basic but here is my question:

so I clone the project and I need to do

git submodule init
git submodule update

and it creates link to external repositories which this project is based on. Now if I want to keep the last version of this code and erase the .git (because we will release it to public soon and don't want to share our git footprints) and still have the "git submodule" working, what do I need to do? In the project folder in addition to the .git, there is a file called ".gitmodules" which contains the links to the external repositories. But when I "rm -rf .git" and do a fresh git init to initialize a fresh git copy, the "git submodule init" and "git submodule update" don't do anything. Should I do something else to make a link between the .git and the ".gitmodules" file that I kept in the folder?

Upvotes: 0

Views: 390

Answers (2)

Anantha Raju C
Anantha Raju C

Reputation: 1907

To remove/erase the .git directory, just delete the .git directory and init again.

(Once you delete the .git folder submodules' commands will not work, since the .gitmodules is deleted along with the .git folder) now you will have achieved your task 1: erase the git directory

To add the submodule to the repository use the commands git submodule add

example: git submodule add https://github.com/chaconinc/DbConnector

If you run git status at this point, you’ll notice a few things. First you should notice the new .gitmodules file. This is a configuration file that stores the mapping between the project’s URL and the local subdirectory you’ve pulled it into.

(If you have multiple submodules, you’ll have multiple entries in this file. It’s important to note that this file is version-controlled with your other files, like your .gitignore file. It’s pushed and pulled with the rest of your project. This is how other people who clone this project know where to get the submodule projects from.)

Now, you will have achieved your task 2: use the submodules as before.

Upvotes: 0

larsks
larsks

Reputation: 312400

If you delete your .git directory and create a new one with git init, you will need to run git submodule add to re-register the submodules. E.g., here we have a repository with some submodules:

bash-4.3$ git submodule
 460317a37795e0751ecc788e571a11fc1a908079 bar (heads/master)
 460317a37795e0751ecc788e571a11fc1a908079 foo (heads/master)

If we delete our git history and re-intiialize the repository:

bash-4.3$ rm -rf .git
bash-4.3$ git init
Initialized empty Git repository in /home/lars/tmp/repo/.git/

At this point, git sees no submodules:

bash-4.3$ git submodule
bash-4.3$ 

Even though they are still there:

bash-4.3$ ls
bar  foo

We can re-register them using git submodule add:

bash-4.3$ git submodule add git@myserver:path/to/repo/foo foo
Adding existing repo at 'foo' to the index
bash-4.3$ git submodule add git@myserver:path/to/repo/bar bar
Adding existing repo at 'bar' to the index

And they're back:

bash-4.3$ git submodule
 460317a37795e0751ecc788e571a11fc1a908079 bar (heads/master)
 460317a37795e0751ecc788e571a11fc1a908079 foo (heads/master)

Upvotes: 1

Related Questions