Demystifying ‘git submodule’ – part 2

In part1 we learnt how we can create submodule and register them inside a git repository and how to fetch sources of git submodule.

Well in this blog I want to revisit init and update command and discuss little more about it.

submodule init

This command reads the .gitmodules files to find out submodule information and puts that information in .git/config file.


[root@kashish tmp]# git clone https://github.com/kashishbhatia/super_project.git
Initialized empty Git repository in /root/git_client/reps/tmp/super_project/.git/
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 1), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.

[root@kashish tmp]# cd super_project/

 ======before issuing 'init' command'====== 
[root@kashish super_project]# cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://github.com/kashishbhatia/super_project.git
[ branch "master"]
        remote = origin
        merge = refs/heads/master

[root@kashish super_project]# git submodule init
Submodule 'src/submodule' (https://github.com/kashishbhatia/dummy_project.git) registered for path 'src/submodule'

 ======after issuing 'init' command====== 
[root@kashish super_project]# cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*
        url = https://github.com/kashishbhatia/super_project.git
[branch "master"]
        remote = origin
        merge = refs/heads/master
[submodule "src/submodule"]
        url = https://github.com/kashishbhatia/dummy_project.git

Note .git/config will only capture url information of submodule.

submodule deinit

This command is complementary of init command. It unregister the submodule from parent repository and remove entries of submodule from .git/config file.

Other way of removing submodule is as follows :


    1. Delete the relevant section from the .gitmodules file.
    2. Stage the .gitmodules changes git add .gitmodules
    3. Delete the relevant section from .git/config.
    4. Run git rm --cached path_to_submodule (no trailing slash).
    5. Run rm -rf .git/modules/path_to_submodule
    6. Commit git commit -m "Removed submodule "
    7. Delete the now untracked submodule files
    8. rm -rf path_to_submodule

 

submodule update

This command reads out the .git/config and .gitmodules file and actually fetches the sources of submodule into submodule path from the submodule URL.


[root@kashish super_project]# git submodule update
Initialized empty Git repository in /root/git_client/reps/tmp/super_project/src/submodule/.git/
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
Submodule path 'src/submodule': checked out 'eb7bcff141a113f81509084be21350afaedc1281'

We can also use git submodule update –init instead of 2-step way of issuing init and the update command.

submodule status

This command is very handy to know whats going on with the submodule used by parent repository; to know whether the sources of submodule are stale and needs update; or does submodule has any changes in your git namespace which are required to be commit; or does it have any merge conflicts.

It displays following symbols in the status when :
– (minus) when submodule is not initialized.
+ when top of the tree commit-id does not match with what is saved in index file.
M when it has merge conflicts.



[root@kashish tmp]# git clone https://github.com/kashishbhatia/super_project.git
Initialized empty Git repository in /root/git_client/reps/tmp/super_project/.git/
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 1), reused 9 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.

 shows '-' sign as submodule is not yet initialized 
[root@kashish super_project]# git submodule status
-eb7bcff141a113f81509084be21350afaedc1281 src/submodule

[root@kashish super_project]# git submodule init
Submodule 'src/submodule' (https://github.com/kashishbhatia/dummy_project.git) registered for path 'src/submodule'

[root@kashish super_project]# git submodule status
-eb7bcff141a113f81509084be21350afaedc1281 src/submodule

[root@kashish super_project]# git submodule update
Initialized empty Git repository in /root/git_client/reps/tmp/super_project/src/submodule/.git/
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
Submodule path 'src/submodule': checked out 'eb7bcff141a113f81509084be21350afaedc1281'

 After updating and checking out sources, it should not show '-' sign. 
[root@kashish super_project]# git submodule
 eb7bcff141a113f81509084be21350afaedc1281 src/submodule (heads/master)

 Note : we can use only 'git submodule' also to know status of submodule 

 

Let see what happens if I add a new file in submodule which is not staged yet.



[root@kashish super_project]# cd src/submodule/

[root@kashish submodule]# touch tmp.c

[root@kashish submodule]# cd ../../
[root@kashish super_project]# git submodule status 
+eb7bcff141a113f81509084be21350afaedc1281 src/submodule (heads/master) 
Note: started showing '+' sign

submodule foreach

This can be used to perform same operation on all the submodules.
Syntax : git submodule foreach

For Example : to pull latest changes done in all submodules registered within a parent repository.



[root@kashish super_project]# git submodule foreach git pull origin master
Entering 'src/submodule'
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/kashishbhatia/dummy_project
 * branch            master     -> FETCH_HEAD
Updating eb7bcff..075f413
Fast-forward
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s