Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Babbleshack/0f99f04d801525cf85cf89f40e0ba161 to your computer and use it in GitHub Desktop.

Select an option

Save Babbleshack/0f99f04d801525cf85cf89f40e0ba161 to your computer and use it in GitHub Desktop.
Submodule notes
# Git Submodules
A git submodule is simply a git repository referenced from another git repository.
Submodule can be modified independantly of their hosting git repository, including merging, commiting, push and pulling.
## Adding a submodule.
Adding a submodule to a repository is easy, simply run `git add submodule <submodule repository url>`.
This will create two new files:
* .gitmodules
lists all submodules of the current git repository and their git urls.
* <directory named after repository>
A directory named after the repository referanced as a submodule -- initially empty.
After creating the submodule, it will need to be initialized.
`git submodule init` this will copy the submodules from .gitmodules to .git/config, enabling your repository.
Next to pull the submodule objects a call to `git submodule update`; this call is analogous to `git pull origin <branch>`
Finally it is important at this point to run `git add -A ./ && git commit -m "adding submodules"` in order to add the submodule directory and `.gitmodules` file.
## Updating submodules.
There are two ways to update a git submodule.
The first method makes us of `git fetch` and `git merge` to manually merge the submodules remote changes into the local submodule.
change to the submodule directory and run `git fetch` in order to fetch the changed objects from the git repository. Then run `git merge` to merge the changes.
The second way automated the submodule update process.
From the parent git directory you may run `git submodule update --remote`
### Branches
If you want to work with a particular branch of a submodule you must make edit either `.gitmodules` or `.git/config`.
The choice of file will affect how the submodule will distributed to other users of the parent repository.
* `git config -f .gitmodules submodule.<submodule_name>.branch <branch_name>`
This will add the branch specifier to the `.gitmodules` file (which is tracked) therefore every user of the parent repository will now pull from teh branch specified.
* `git/config`
Making the change here will effect only your own copy of the submodule.
# Referance
https://git-scm.com/book/en/v2/Git-Tools-Submodules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment