Example code of adding, using and deleting submodule in Git

Time:2020-9-2

background

The project often uses the module maintained by others. Using the function of sub module in Git can greatly improve the development efficiency.

After using the sub module, you don’t have to be responsible for the maintenance of the sub module, only need to update the sub module synchronously when necessary.

This paper mainly explains the basic commands related to the sub module. Please refer to man page for details.

Add sub module

Adding a sub module is very simple. The command is as follows:


git submodule add <url> <path>

Where URL is the path of the submodule, and path is the directory path of the submodule.

After successful execution, GIT status will see that. Git modules have been modified in the project and a new file (for the path just added) has been added

git diff --cachedViewing the modified content, you can see that the sub module has been added, and the submitted hash summary of the sub module is under the new file

git commitSubmit to complete the addition of sub modules

Use of sub modules

After cloning the project, there is nothing in the default submodule directory. You need to execute the following command in the root directory of the project to download the sub module:


git submodule init
git submodule update

Or:


git submodule update --init --recursive

After execution, the source code will be available in the sub module directory, and then the corresponding makefile can be executed.

Update of sub modules

After the submodule maintainer has submitted the update, the project using the submodule must be manually updated to include the latest commit.

In the project, go to the sub module directory, execute git pull update, view git log and view the corresponding submission.

When finished, return to the project directory to see the updates to be submitted by the sub module. Use git add to submit.

Delete submodule

Sometimes the project maintenance address of the sub module changes, or the sub module needs to be replaced, the original sub module needs to be deleted.

It is complicated to delete sub modules, and the steps are as follows:
1. RM – RF sub module directory delete sub module directory and source code
2. Vi. Gitmodules delete submodule related entries in. Gitmodules file under the project directory
3. Vi. Git / config delete sub module related entries of configuration item
4. RM. Git / module / * delete the sub module directory under the module. Each sub module corresponds to a directory. Note that only the corresponding sub module directory can be deleted

After execution, execute the add sub module command again. If there is still an error, execute the following:

git rm --cached Submodule name

After deletion, submit it to the warehouse.

This article on the GIT submodule add, use and delete the sample code to introduce this, more related git submodule add and delete content, please search the previous article of developeppaer or continue to browse the related articles below, I hope you can support developeppaer more in the future!