Recently, I am making an upload client, and the upload part is provided by the backend students in the form of SDK, so the SDK is in an independent warehouse, so how to integrate the SDK conveniently for the client? Copy code to client repository every SDK update? Release the SDK to NPM? Consider using Git’s submodule.

What is a submodule

Submodule is a multi-project management tool that allows subprojects to be added as separate Git projects to a master project that owns subprojects in the form of submodules. Subprojects have their own commits, pushes, and pulls, independent of the main project. The master project only needs to record the address of the subproject and the required COMMIT ID to get the corresponding subproject from the address and COMMIT ID.

Add submodule

Normally, we have a MainProject, and you can add submodules by executing the following command in the MainProject folder.

*/ $git submodule add [url] [path] example git submodule add [email protected]:fengyueran/UploaderSDK.git ./src/UploaderSDKCopy the code

Git status You can view the following information

 On branch master
    Changes to be committed:

        new file:   .gitmodules
        new file:   UploaderSDK
Copy the code

You can see two more files:.gitModules and UploaderSDK. Cat.gitmodules See. Gitmodules stores the path and remote address of the subModule.

[submodule "src/uploaderSDK"]
	path = src/uploaderSDK
	url = [email protected]:fengyueran/UploaderSDK.git
Copy the code

The content of UploaderSDK is the COMMIT ID of subModule.

Subproject commit 6b53e1840b27ca1587b96c1eb9dd5f4ff0866089
Copy the code

It’s not hard to imagine getting the contents of the SubModule from.gitModules and UploaderSDK, so we need to submit these two files.

git add .
git commit -m "add submodule"
Copy the code

Clone the project with subModule

There are two main ways

1. Clone first and update later

When you clone the main project directly, the subModule will not follow the clone, but will be empty folder containing the subModule name.

1)$ git clone [email protected]:fengyueran/MainProject.git
Copy the code

Run the following command again

$git submodule initCopy the code

In the output below, you can see that the command registers the path to the subproject, the location in the main project. At this point, the uploaderSDK folder is not empty.


    Submodule 'src/uploaderSDK' ([email protected]:fengyueran/UploaderSDK.git) registered for path 'src/uploaderSDK'
Copy the code

To perform

This command does not update directly to the latest SubModule COMMIT, but to the commit stored in the main project (possibly an older commit). $git submodule updateCopy the code

In the output below, you can see that the suModule is updated to the subModule commit in the main project store, which is a free Git header.

Cloning into '/Work/test/MainProject/tmp/MainProject/src/uploaderSDK'. Submodule path'src/uploaderSDK': checked out '6b53e1840b27ca1587b96c1eb9dd5f4ff0866089'
Copy the code

2. Use recursive parameters –recursive

git clone [email protected]:fengyueran/MainProject.git --recursive
Copy the code

In the output below, you can see that the main project including the Submodule has been cloned.

Cloning into 'MainProject'. remote: Counting objects: 7, done. remote: Compressing objects: 100% (4/4), done. Receiving objects: 100% (7/7), done. remote: Total 7 (delta 0), reused 4 (delta 0), pack-reused 0 Submodule'src/uploaderSDK' ([email protected]:fengyueran/UploaderSDK.git) registered for path 'src/uploaderSDK'
Cloning into '/Work/test/MainProject/tmp/MainProject/tmp/MainProject/src/uploaderSDK'. Submodule path'src/uploaderSDK': checked out '6b53e1840b27ca1587b96c1eb9dd5f4ff0866089'
Copy the code

Modified Updated the SubModule

There are two main cases

1. Modify directly under subModule in the main project

MainProject uploaderSDK (SRC /uploaderSDK); MainProject uploaderSDK (SRC /uploaderSDK); The SRC /uploaderSDK commit has changed from the current commit to the current one (if you want to test other commits, you can switch to the corresponding commit)

-Subproject commit 6b53e1840b27ca1587b96c1eb9dd5f4ff0866089
+Subproject commit a4d6dc0457673a275b1f6cbeda6f8ff23293b9de
Copy the code

Git subModule update (a4d6) in MainProject If you do this, the subModule goes back to the original COMMIT (commit with minus sign), just commit at MainProject and push to the remote repository if necessary. This way, non-subModule developers don’t care if the subModule is updated, You only need to execute git subModule update(with a minus commit) when the pull code under MainProject detects changes to the subModule, if the other developers have committed the correct subModule COMMIT.

2. Modify the subModule in its own warehouse

The repository of the subModule under the working directory clone is switched to the working branch for modification submission and push to the remote repository. This approach requires the subModule developer to either tell the MainProject developer that the subModule is updated or actively check for updates. UploaderSDK will pull the remote code from MainProject SRC /uploaderSDK. After a quick merge, uploaderSDK’s commit changes as follows: Do not git submodule update at this time, just commit the change under MainProject.

-Subproject commit f4573cc1bb50000779202c7f56a640b1ffc075cb
+Subproject commit 64ae6d149c0f6e3b06b8cea262c6126a7bc0887f

Copy the code

Delete the submodule

Run the following command

  1. $ git submodule deinit
$git subModule deinit [submodule_name] -> Cleared directory'test2sub'
Submodule 'test2sub' ([email protected]:fengyueran/test2sub.git) unregistered for path 'test2sub'/ / execute the following commands can also see $git submodule - > - dab52c62f52353d9967619625c28e28dc4320aef component informationtest2sub
Copy the code
  1. $ git rm --cached [submodule_name]
Git rm --cached git rm --cached git rm --cachedtest$git submodule $git submoduleCopy the code
  1. $ git commit
git commit -m "remove submodule"
Copy the code