Example how to git submodule, add, delete submodule and use existed git repository


Git submodule is great feature on Git which it will needed much on large projects than build by small parts. For example, I have single project called “Insurance”. This project contains several apps like “billing”, “user”, “backend” and several apps. We want to create single repository that contain several small repositories.

Using git submodule will ease for integration with several programmer or applications. In this example, I use gitolite which you can follow this article to Install Gitolite in Ubuntu. I will create 3 repositories called “insurance”, “billing” and “user”.

Now I clone master projects called “insurance” as Insurance folder, eg:

1
git clone git@ubuntu:insurance Insurance

I want insurance have nested folder which contain from another repository. eg :

1
2
3
4
5
6
Insurance
    |_______ Billing [ Another Git Repo ]
    |
    |_______ Maintenance {folder only}
                        |
                        |___ User [ Another Git Repo ]

Then because I have “billing” and “user” repo already, So, I just make this both repositories as sub-module of “Insurance” repository.

1
2
3
cd Insurance
git submodule add git@ubuntu:billing Billing
git submodule add git@ubuntu:user Maintenance/User

Remember, to do sub-module, you should go to root path of main repository. If you need to place sub-module into nested folder, then you can add into “your_nested_path/your_submodule” which have same example as User repository. Make sure you already make first commit (initial) each git repository which it will set it default master branch automatically.

If you got problem like this :

1
2
fatal: You are on a branch yet to be born.
Unable to checkout submodule ‘fpl/airline’

That mean you should not commit / have empty repository on sub-module git. Please commit your sub-module first before adding into master. Also, make sure you have commit the master repository.

Example success adding sub-module, eg :

1
2
3
4
5
6
git submodule add git@ubuntu:user Maintenance/User
Cloning into Maintenance/User…
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 22 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (22/22), 19.77 KiB, done.

To remove your sub-module :

1
2
3
4
Delete the relevant line from the .gitmodules file.
Delete the relevant section from .git/config.
Run git rm –cached path_to_submodule (no trailing slash).
Commit and delete the now untracked submodule files.

You should rememmber this every update or commit on repository which use sub-module :

Always publish the submodule change before publishing the change to the superproject that references it.

Everytime you do ” git submodule update “, please check branch status by “git status” or your file will silently overwritten.

Not add a trailing slash when specifying the submodule path.

How to update all sub-module ?

First, go to root path of project and do :

1
git submodule status

Will show the results :

1
2
7222f8f705e887665815dca11253ed4181a4ad3b Billing (heads/master)
25c7834a0f0b093485b3faf18bcb8b7bf6d914e6 Maintenance/User (heads/master)

Now we update all submodule together by :

1
git submodule foreach git pull

Example result :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Entering ‘Billing’
Already up-to-date.
Entering ‘Maintenance/User’
remote: Counting objects: 9, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 8 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.
From ubuntu:user
   2be90b9..82da25b  master     -> origin/master
Updating 2be90b9..82da25b
Fast-forward
 .project                                |   17 ++++++
 .pydevproject                           |   10 ++++
 README.md                               |   13 +++++
 3 files changed, 130 insertions(+), 0 deletions(-)
 create mode 100644 .project
 create mode 100644 .pydevproject
 create mode 100644 README.md

How if we want edit files inside sub-module from master / root projects ?
It’s easy, you just go to subfolder, make changes and git push origin master.
It will updated into submodule repository.

1
2
3
4
5
6
7
8
9
10
git add -A && git commit -m ‘change sub-module files from master’
git push origin master

Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 788 bytes, done.
Total 5 (delta 1), reused 0 (delta 0)
To git@ubuntu:user
   8acc073..47dea5f  master -> master

It’s fun using git submodule 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.