Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create submodule in existing repo

I'm beginner to git & I have the following folder structure for new project, so what I did is I went to visual studio and I made empty project it has this folder structure:

project: (Repo) 
folder1 (sub1)
folder2 (sub2)

I'm using git locally.
I created a repo for project using sourcetree,
now I cannot add any submodule, I don't know why.

In powershell whenever I try:

PS E:\Projects\Project> git submodule add ./sub1

I get the following error:

sub1 already exists in the index

then I decided to remove sub1 using git rm -r sub1,and add it again, then when I tried to add it again using git submodule add again, I received this new error:

sub1 already exists and is not a valid git repo

So what I'm doing wrong?

like image 254
ma1169 Avatar asked Feb 21 '16 10:02

ma1169


People also ask

Can you push to a submodule?

In the parent repo, you can also use git push --recurse-submodules=check which prevents pushing the parent repo if the submodule(s) are not pushed first. Another option is git push --recurse-submodules=on-demand which will try to push the submodules automatically (if necessary) before pushing the parent repo.

Can you commit to submodule?

You can also change the commit that is checked out in each submodule by performing a checkout in the submodule repository and then committing the change in the parent repository. You add a submodule to a Git repository via the git submodule add command.

How do I add a submodule to bitbucket?

Add git submodule Next we will add a submodule to this fresh new repo. $ git submodule add https://bitbucket.org/jaredw/awesomelibrary Cloning into '/Users/atlassian/git-submodule-demo/awesomelibrary'... remote: Counting objects: 8, done. remote: Compressing objects: 100% (6/6), done.


1 Answers

You simply need to be in your root folder and then add the submodule folder.

git submodule add <url>

Now when you clone the project you simply need to init and update the submodule

git submodule init
git submodule update

Git 1.8.2 features a new option --remote

git submodule update --remote --merge

will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule. As [the docs][1] put it:

--remote

This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

This is equivalent to running git pull in each submodule.

like image 89
CodeWizard Avatar answered Sep 27 '22 18:09

CodeWizard