Problem scenario
Believe in any development, there will be a situation. Do different projects, but will use some common methods _ components _ code blocks and so on. As an elegant developer, you can’t accept a piece of code being copied and pasted everywhere. And if this code needs to be updated later, pasting it around will require a global search and tearful modification. Is there a way to write in one place and use it everywhere as a habitat for some common code?
The answer is yes.
Looking for tools
After searching the well-known 404 website, I found a built-in feature of Git: subModule.
What is a submodule
We often encounter situations where a project at work needs to include and use another project. Maybe it’s a third-party library, or one you developed independently for multiple parent projects. Here’s the problem: you want to treat them as two separate projects, but at the same time you want to use the other in one project.
Git solves this problem with submodules. Submodules allow you to use one Git repository as a subdirectory of another Git repository. It allows you to clone another repository into your own project while keeping the commit independent.
How to use
Adding submodules
# Clone directly, In the current directory to generate a someSubmodule directory content storage warehouse git submodule add https://github.com/chaconinc/someSubmodule # git submodule add file directory https://github.com/chaconinc/someSubmodule src/submodulePathCopy the code
After the addition is successful, running Git status will find two changes added to the parent repository
- new file: .gitmodules
- New file: someSubmodule (not actually a file)
To expand:
-
Submodules. Submodules is a file that records the submodule configuration of the current project, which holds the mapping between the project URL and the local directory that has been pulled.
-
Git status after adding submodules, you will see information similar to the following
$ git diff --cached someSubmodule
diff --git a/someSubmodule b/someSubmodule
# Key is the following line 160000new file mode 160000 index 0000000.. C3f01dc - + + + b / / dev/null DbConnector @ @ - 0, 0, + 1 @ @ + Subproject commit c3f01dc8862123d317dd46284b05b6892c7b29bcCopy the code
Although someSubmodule is a directory in the parent repository, Git does not list all of its changes, but treats them as a special commit. PS: 160,000 mode. This is a special mode in Git, which essentially means that you record a commit as a directory record, rather than as a subdirectory or a file.
Clone already includes submodule projects
Normal Clone contains submodule functions after someSubmodule is automatically generated due to the presence of the. Submodule file. But it was empty. Two more commands need to be executed.
# to initialize the local configuration file
git submodule init
Grab all data from this project and check out the appropriate commits listed in the parent project (specified commits).Git submodule update -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- better way -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -# clone the parent repository with --recursive will automatically initialize and update each submodule in the repository
git clone --recursive https://github.com/chaconinc/MainProject
Copy the code
Git subModule workflow
When a project contains submodules, it is not only necessary to version the parent repository, but also to have versions in the submodule directory. The problem of how to version submodules in different parent repositories becomes a new one.
The simplest way to do this is for the main project to focus only on the versions on the master branch of the submodule and not on any branch versions within the submodule.
The operation is as follows:
cd submodulePath
git fetch
git merge origin/master
Copy the code
You can see in the main project that the SubModule directory has been updated. Of course, this is also a little inconvenient to operate, but here is a simpler method:
Git submodule update --remote Git submodule update --remoteCopy the code
If you need to update other branches, you need to configure them separately.
Set git submodule update --remote to stable
git config -f .gitmodules submodule.DbConnector.branch stable
Copy the code
Matters needing attention
To be continued
Focus on giving encouragement
Reference Documents:
- Git – sub-module