Özgün Koyun Özgün Koyun

Working with git submodules

  linux git

Cloning the main project

$ cd ~
$ git clone git@github.com:ozgun/main_project.git

Initializing the submodule (sub project):

Create ~/main_project/.gitmodules file and add the followings lines to it:

[submodule "sub_project"]
        path = sub_project
        url = git@github.com:ozgun/sub_project.git

Run the following commands to initialize the submodule:

$ cd ~/main_project
$ git submodule init
$ git submodule update

“git submodule update” command does not checkout to “master” branch. So we need to create master branch and then switch to it as below:

$ cd ~/main_project/sub_project
$ git checkout master

When something changes in sub project

Since sub_project is a submodule in main_project, when something changed in submodule, main projects also needs to be notified(synced) about this change.

Adding a new file to submodule and pushing to sub_project’s repository

$ cd ~/main_project/sub_project
$ git checkout master # make sure you are working on master branch
$ touch README
$ git add README
$ git commit -am "Added README to sub_project - submodule update"
$ git push origin master

Pushing the main_project to remote repository

After we updated sub_project, we need to update main_project too.

$ cd ~/main_project
$ git checkout master
$ git status

After you run “git status” you should see something like this:

# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#       modified:   sub_project
no changes added to commit (use "git add" and/or "git commit -a")

Your main projects understands that something has changed inside the sub_project, so it is considered as “modified”. This means that we need to commit and then push to remote git repository as below:

$ cd ~/main_project
$ git add sub_project # Do not append "/" !
$ git commit -am "Submodule sub_project updated"
$ git push origin master