This is the second day of my participation in Gwen Challenge
Usage scenarios
As more and more corporate-based projects grow, it is often necessary to extract a common library for multiple projects to use, but how can this library and Git be easily managed together?
It is common in normal software development to have a module that you are responsible for depend on another module or a third party library. In this case, your module is a separate repository, and you want to implement such a feature that when you pull code from your module’s repository to the local directory, you can automatically pull your dependent modules or third-party libraries to the specified directory.
We need to solve the following problems:
- How do I import in git projects
The library library
? The library library
Is it modified in another project and can be updated to a remote code base?- How can other items be obtained
The library library
The latest submission? - How can I import automatically during Clone
The library library
?
To solve these problems, consider using Git Submodule.
What is a Submodule
Git Submodule is a great tool for multiple projects to use a common class library. It allows the class library project to be a repository and the subproject to be a separate Git project within the parent project. The subproject can have its own independent commit, push, and pull projects. The parent project contains subprojects in the form of Submodule. The parent project can specify subproject headers. The submission information of the parent project contains Submodule information, and the Submodule can be initialized when the parent project is cloned.
The specific use case
There is currently A module A with A repository address of demo_project_a.git that needs to reference another module B with A repository address of module_B.
New submodule
Assume that the local directory of module A is demo_project_a
You want to reference module B as A submodule of module A. Its path in module A is demo_project_A/module_B
➜ submodule_test ll total 0 drwxr-xr-x 5 Mervinwang staff 160B 5 24 14:24 demo_project_a ➜ submodule_test tree. ├ ─ ├─ └─ Readme.txt TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXT TXTCopy the code
This is done through git’s submodule mechanism.
For example, in the command line, you can directly use the following command:
➜ submodule_test CD demo_project_a ➜ demo_project_a git:(master) qualify git submodule add http://159.75.195.153/root/module_b.git module_b in 'module_b' existing warehouse is added to the indexCopy the code
Note: Submodule subdirectories cannot be specified with a “/”. For example, the command above cannot be written as projectB/
After running this command, execute git status in the projectA directory and you can see the following result:
➜ demo_project_a git:(master) qualify git status your branch goes onto the 'origin/master' branch. Changes to commit: (Use "git restore --staged < file >..." To cancel staging) new file:.gitModules New file: module_BCopy the code
In this case, you need to use git commit and git push to push the result of adding module B as A submodule of module A into the repository of module A.
➜ demo_project_a git:(master) qualify git commit -m "add module_b" [master 831e9ee] add module_b 2 files changed, 4 insertions(+) create mode 100644. Gitmodules create mode 160000 module_b ➜ demo_project_a git:(master) git push origin Master enumerate object: 4, done. Object count: 100% (4/4), done. Use 12 threads to compress the compressed object: 100% (3/3), complete. Write objects: 100% (3/3), 379 - byte | KiB/s, 379.00. A total of 3 differences (0), multiplexing 0 0 (difference), packet multiplexing 0 01 d8e53 To http://159.75.195.153/root/demo_project_a.git.. 831e9ee master -> masterCopy the code
Cloning submodule
1 Clone warehouse demo_projecy_A
[~/Tencent/Code/submodule_test]$ git clone http://159.75.195.153/root/demo_project_a.git
Cloning into 'demo_project_a'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), 544 bytes | 136.00 KiB/s, done.
[~/Tencent/Code/submodule_test]$ ls
demo_project_a
[~/Tencent/Code/submodule_test]$ tree.├ ── ├─ 2nd Exdirectories, 1 fileCopy the code
In this case, the file in SubModule MODULE_B is empty
[~/Tencent/Code/submodule_test]$ cd demo_project_a
[~/Tencent/Code/submodule_test/demo_project_a]$ git submodule initSubmodule 'module_b' (http://159.75.195.153/root/module_b.git) registered for the path 'module_b'[~/Tencent/Code/submodule_test/demo_project_a]$ git submodule update
Cloning into '/Users/mervinwang/Tencent/Code/submodule_test/demo_project_a/module_b'...
Submodule path 'module_b': checked out 'da5cef17a5e650135f77466fce3cdfda3fdcaae9'
[~/Tencent/Code/submodule_test/demo_project_a]$ tree.├ ── ├.txt, 2 filesCopy the code
Delete the submodule
-
Deleting directory Files
[~/Tencent/Code/submodule_test]$ cd demo_project_a [~/Tencent/Code/submodule_test/demo_project_a]$ ls README.md module_b [~/Tencent/Code/submodule_test/demo_project_a]$ rm -fr module_b [~/Tencent/Code/submodule_test/demo_project_a]$ rm -fr .gitmodules Copy the code
-
Delete the config
vim .git/config [submodule "module_b"] url = http://xxxxxxx/root/module_b.git Copy the code
-
Delete the subModule-related content and submit it to the remote server
[demo_project_a] git commit -a -m 'remove module_B submodule' Master University [Master 9BE913f] remove module_B submodule 2 files changed, 4 deletions(-) delete mode 100644 .gitmodules delete mode 160000 module_b [demo_project_a] git push master Enumerating objects: 3, done. Counting objects: 100% (3/3), done. Delta compression using up to 12 threads Compressing objects: 100% (1/1), done. Writing objects: 241.00 100% (2/2), 241 bytes | KiB/s, done. Total 2 (delta 0), reused zero (0) delta, Pack - 831 e9ee reused 0 To http://159.75.195.153/root/demo_project_a.git.. 9be913f master -> masterCopy the code