Working with Git Submodule

 What is Submodule ?

  • Concept of adding repository within another repository 
  • project within a project
  • submodule lives within your repository and you have full access to it instead of distribution

When/why ?

  • Sharing Libraries 
  • You have a full access to it, you can customise it 

Using : 

# add a repository to your submodule

$ git submodule add [your_repository_url_link]

We get 2 things: 

1. 

[submodule "your-library-android"]
path = your-library-android
url = https://www.com/app/your-module/yourlibraryandroid.git
2. [your-library-android] module 

One of the things that we don't see is the dependency version whenever you commit changes. 

We don't have explicit commit hash in submodule repository but it is explicitly picked up by git. 

We can see this commit hash directly from the GitHub repository site. 

Lifecycle of Submodule Vs Lifecycle of Standard Repository ?

  • By default when you use git commands it will ignore all submodules
  • Ex. if you clone a repository, it wont clone submodules 
  • Create Branch or commit , the submodules are ignored by default 
$ git pull -- recursive-submodules 
# this command will do pull and apply command to each of the submodules 

# if you don't wanna type this command each time you can add this command in config to set true so each time you write a git command it is executed to all submodules 
$ git config submodule.recurse true 

#Initially when you clone the submodule, the folder is empty so you can use this command to bring them in
$ git submodule update --init

# checkout the master of the current repository where you are launching your command 
$ git checkout master 

# check the logs of each commit 
$ git log 


# Your submodule 

$ cd [Submodule Path]

$ git add . 

$ git status 

$ git commit -m “Commit Message” 

$ git push 

$ git log 



# incase you remove the project and wanna reinitialise it 

$ git submodule add --force [your_repository_link]


# for pulling incase of more than one submodule 

$ git submodule foreach git pull 








Comments

Popular Posts