The child module
We often encounter situations where a project at work needs to include and use another project. Git subModule: Git subModule: Git subModule: Git subModule: Git submodule: Git submodule: Git submodule: Git submodule
add
Start by adding an existing Git repository as a submodule. You can use the
Git subModule add Repository address pathCopy the code
- The warehouse address refers to the sub-module warehouse address
- Path refers to the path to place the submodule under the current project. By default, the submodule is placed at the same level as the main project. If you want to set other locations, you need to specify the path
Note: The path cannot end with/(the modification will not take effect), and cannot be an existing directory of the existing project (it cannot be cloned successfully).
The command is followed by the relative or absolute URL of the item you want to track to add a new submodule. In this case, we will add a library named “DbConnector”.
$ git submodule add https://github.com/chaconinc/DbConnector
Cloning into 'DbConnector'...
remote: Counting objects: 11, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 11 (delta 0), reused 11 (delta 0)
Unpacking objects: 100% (11/11), done.
Checking connectivity... done.
Copy the code
After the command is executed, a file named. Gitmodules is generated in the root path of the current project, which records information about submodules
[submodule "DbConnector"]
path = DbConnector
url = https://github.com/chaconinc/DbConnector
Copy the code
Submodule changes need to be submitted to the Git repository of the submodule and returned to the main project to update the Module
delete
Deleting submodules is a bit trickier: First, you need to delete the configuration information in the. Gitmodules file. Then, run git rm -cached to delete the submodule file from Git.
Clone the project with submodules
-
Clone a project with submodules. When you clone such a project, the submodule directory is included by default, but the submodule does not have any files
You must run two commands: git submodule init to initialize the local configuration file, and git subModule update to grab all data from the project and check out the appropriate commits listed in the parent project.
At this point, the submodules are fully cloned
-
There’s an easier way. If you pass the –recurse-submodules option to the Git clone command, it automatically initializes and updates every submodule in the repository, including any nested submodules that might exist.
Such as:
git clone --recurse-submodules https://github.com/chaconinc/MainProject Copy the code
-
If you already cloned the project but forgot –recurse-submodules
You can run git subModule update –init to merge git submodule init and Git submodule update in one step. If you also want to initialize, grab, and check out any nested submodules, use the concise Git subModule update –init –recursive
-
After the local subproject is modified, if the master project is not pushed, then the master project is directly pushed to the remote repository, which will cause other people’s pull code cannot run, because the changes of the subproject cannot be obtained. In this case, before the master project pushes, it is necessary to check that all the changes of the subproject are pushed successfully
Git push accepts –recurse-submodules, which can be set to check or on-demand. If any submitted submodule changes are not pushed, the “check” option will directly fail the push operation.
git push --recurse-submodules=check Copy the code
If you want to check all your pushes, you can make this the default behavior
git config push.recurseSubmodules check Copy the code
In this case, if the subproject push fails, the main project will also fail. If you do not want to check, you can use on-demand to restore the default status
git config push.recurseSubmodules on-demand Copy the code
Submodule technique
Submodule traversal
There is a foreach submodule command that can run arbitrary commands in each submodule. This is useful if your project includes large quantum modules.
For example, save the current progress of all submodules
git submodule foreach 'git stash'
Copy the code
The alias
You may want to set aliases for some of these commands because they can be very long and you can’t set options as their default. We covered setting Git aliases in Git Aliases, but here are some examples if you plan to use a lot of submodules in Git
$ git config alias.sdiff '! '"git diff && git submodule foreach 'git diff'" $ git config alias.spush 'push --recurse-submodules=on-demand' $ git config alias.supdate 'submodule update --remote --merge'Copy the code
\