There will be more and more company based projects, and it is often necessary to extract a public class library for multiple projects, but how can this library be easily managed with git?
We need to solve the following problems:
How do I import the library library in a git project?
The library library has been modified in other projects and can be updated to the remote code library?
How can other projects get the latest submission of the library library?
How can I automatically import the library library when I clone?
To solve the above problems, you can consider using git Submodule.
What is a Submodule?
git Submodule It is a good tool for multiple projects to use a common class library. It allows the class library project as a repository, and the sub project as a separate git project to exist in the parent project. The sub project can have its own independent commit, push and pull. The parent project contains sub projects in the form of submodule. The parent project can specify the sub project header. The submission information in the parent project contains the submodule information. When cloning the parent project, the submodule can be initialized.
Using Submodule in a project
How to use
1. Create a version library with modules
For example, we want to create a project with the following structure
project |--moduleA |--readme.txt
Create a project version library and submit the readme.txt file
git init --bare project.git git clone project.git project1 cd project1 echo "This is a project." > readme.txt git add . git commit -m "add readme.txt" git push origin master cd ..
Create the moduleA version library and submit the a.txt file
git init --bare moduleA.git git clone moduleA.git moduleA1 cd moduleA1 echo "This is a submodule." > a.txt git add . git commit -m "add a.txt" git push origin master cd ..
Introduce the sub module moduleA into the project and submit the sub module information
cd project1 git submodule add ../moduleA.git moduleA git status git diff git add . git commit -m "add submodule" git push origin master cd ..
You can see two more files to be submitted by using git status. gitmodules specifies the main information of the submodule, including the path and address information of the submodule. moduleA specifies the commit id of the submodule. You can see the contents of these two items by using git diff. It should be pointed out here that the GIT of the parent project does not record the file changes of the submodule. It specifies the git header of the submodule according to the commit id, so. gitmodules and module a need to be submitted to the remote warehouse of the parent project.
On branch master Your branch is up-to-date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: .gitmodules new file: moduleA
2. Clone the version Library of the tape module
Method 1: first clone the parent project, then initialize the submodule, and finally update the submodule. Initialization only needs to be done once, and then directly update each time. Note that the submodule is not on any branch by default, and it points to the submodule commit id stored in the parent project.
git clone project.git project2 cd project2 git submodule init git submodule update cd ..
Method 2: adopt the recursive parameter -- recursive. Note that the same submodule is not on any branch by default, and it points to the submodule commit id stored in the parent project.
git clone project.git project3 --recursive
3. Modify sub module
After modifying a sub module, it will only affect the version Library of the sub module and will not affect the version Library of the parent project. If the parent project needs to use the latest sub module code, we need to update the submodule commit id in the parent project. By default, we can see that the submodule commit id in the parent project has changed by using git status, We just need to submit it again.
cd project1/moduleA git branch echo "This is a submodule." > b.txt git add . git commit -m "add b.txt" git push origin master cd .. git status git diff git add . git commit -m "update submodule add b.txt" git push origin master cd ..
4. Update sub module
When updating sub modules, it should be noted that the branch of sub modules is not master by default.
Method 1: first pull the parent project, and then execute git submodule update. Note that the branch of module a is never the master.
cd project2 git pull git submodule update cd ..
Method 2: first enter the sub module, then switch to the required branch, here is the master branch, and then pull the sub module. This method will change the branch of the sub module.
cd project3/moduleA git checkout master cd .. git submodule foreach git pull cd ..
5. Delete sub module
There are many ways to use this method on the Internet
git rm --cached moduleA rm -rf moduleA rm .gitmodules vim .git/config
Delete submodule related content, such as the following
[submodule "moduleA"] url = /Users/nick/dev/nick-doc/testGitSubmodule/moduleA.git
Then submit to the remote server
git add . git commit -m "remove submodule"
However, during my own local experiments, I found that the following methods can also be used. The server records. gitmodules and moduleA. As long as you delete moduleA locally with git's delete command, and then check the status with git status, you will find that both. gitmodules and moduleA have changed. As for. git/config, the submodule information will still be recorded, However, there is no impact on local use. If you clone from the server again, there will be no submodule information in. git/config.
git rm moduleA git status git commit -m "remove submodule" git push origin master
References and excerpts from the original text: