Using Git submodule to manage sub-modules

Source: Internet
Author: User
Tags commit pack parent directory using git git clone

Instance code: Parent Project: Https://github.com/jjz/pod-project Sub-project: Https://github.com/jjz/pod-library usage Scenario

Based on the company's multi-project, we extracted a common class library for use in multiple projects, but how can this library be easily managed with git? Here are a few questions to solve: how do I import library libraries in a git project? Library libraries have been modified in other projects how to push? How do other projects get the latest submissions to the library? How can I automatically import library libraries when I clone?

To solve the above problem, I use Git's submodule to solve. What is Submodule?

Git submodule is a great project collaboration tool that allows a class library project to be a repository, a subproject that exists in the parent project as a separate Git project, and the subproject can have its own independent commit,push,pull. The parent project contains the subproject as a submodule, the parent project can specify the subproject header, the parent project submits the Submodule information, and the submodule can be initialized when the parent project is clone. add submodule to your project

git submodule add git@github.com:jjz/pod-library.git pod-library

Use the git status command to see

git status on


branch master
changes to be committed:

    new file:   . gitmodules
    New file:   Pod-library

Two more files to submit
Gitmodules Content

[Submodule "Pod-library"]
    Path = pod-library
    url = git@github.com:jjz/pod-library.git

This records the contents of the subproject and the GIT information for the child project.

Pod-libray
Subproject Commit 4AC42D2F8B9BA0C2F0F2F2EC87DDBD529275FEA5

Here is the commit of the subproject Id,git does not record submodule file changes, it is according to this commit git to compare submodule changes

All two files need to be committed to the parent project's Git

We can also add submodule

git Add. gitmodules pod-ibrary
 git commit-m "pod-library submodule"
 git submodule init
Modify Submit Submodule

First, confirm that you have commit permission to submodule.
Enter the Submodule directory to submit the modified file

CD pod-library/

We modified one of the files to see the changes in the file.

Git status

    modified:   pod-library/useafhttp.h

Commit Submodule

Git commit-a-m ' Test submodule '

Push to remote
>git push

Then go back to the parent directory:

CD..
git status on
branch master


    modified:   pod-library (New commits)

You can see that pod-library has changed to a more recent commit ID

Subproject Commit 330417cf3fc1d2c42092b20506b0d296d90d0b5f
We need to push to the far end of the parent project.

git commit-m ' update submodule '
git push
Update Submodule

There are two ways to update: Run in the parent project's directory

Git submodule foreach git pull

Updated under the Submodule directory

CD Pod-library
Git pull

Note When updating submodule, if a new commit ID is generated and a new commit is generated in the parent project, the subproject commit in the Pod-libray file becomes the latest commit ID Initialize submodule in clone with recursive parameters--recursive

git clone git@github.com:jjz/pod-project.git--recursive

Loning into ' pod-project '
... Remote:counting objects:57, done.
Remote:compressing objects:100% (45/45), done.
Remote:total (Delta), reused (Delta 8), pack-reused 0
receiving objects:100% (57/57), 18.79 KiB | 0 bytes/s , done.
Resolving deltas:100% (13/13), done.
Checking connectivity
... done. Submodule ' Pod-library ' (git@github.com:jjz/pod-library.git) registered for path ' pod-library '
Cloning into ' Pod-library '
... Remote:counting objects:34, done.
Remote:compressing objects:100% (25/25), done.
Remote:total (Delta 8), reused (Delta 7), pack-reused 0
receiving objects:100% (34/34), 12.95 KiB | 0 bytes/s , done.
Resolving deltas:100% (8/8), done.
Checking connectivity
... done. Submodule path ' pod-library ': Checked out ' 330417cf3fc1d2c

42092b20506b0d296d90d0b5f '

will automatically init submodule

Or use the second method
Clone Parent Project First

git clone git@github.com:jjz/pod-project.git
CD Pod-project
Git submodule init

Submodule ' Pod-library ' (git@github.com:jjz/pod-library.git) 
registered for path ' pod-library '

git submodule update

Cloning into ' pod-library '
... Remote:counting objects:34, done.
Remote:compressing objects:100% (25/25), done.
Remote:total (Delta 8), reused (Delta 7), pack-reused 0
receiving objects:100% (34/34), 12.95 KiB | 0 bytes/s, Done.
Resolving deltas:100% (8/8), done.
Checking connectivity
... done. Submodule path ' pod-library ': Checked out ' 330417cf3fc1d2c42092b20506b0d296d90d0b5f '
Delete Submodule

GIT does not support direct deletion submodule need to manually delete the corresponding file

CD pod-project
git rm--cached pod-library
rm-rf pod-library
rm. Gitmodules

 vim. Git/config
  [ Submodule "pod-library"]
      url = git@github.com:jjz/pod-library.git
   delete submodule related content
 git commit-a-M ' Remove Pod-library submodule '


Author: Kang Jia Zhi
Links: Http://www.jianshu.com/p/d433d3417a19
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.