Git submodule Note

add submodule

1
git submodule add --force gitr:service_rpc/idl idl

If this repo has not previously been used submodule then Git will create a directory in the directory called .gitmodules , which records the URL of the remote repo and the submodule in this project path.

After executing this command submodule and .gitmodules will automatically staged, this time can commit and push.

Update submodule

Individual repo update is too much trouble, it is necessary to separate directory under execution git pullto pull the upstream code, but that would be more secure; all at once to update all of submodule can use this foreachcommand:

1
git submodule foreach --recursive git pull origin master

Remove submodule

I thought there will be like git submodule rmsuch an instruction, the result is not even, you must manually remove the hard ground one by one, not knowing what not to implement this directive considerations, I hope that future versions can add to it.

Remove submodule has the following steps to do, first remove the submodule directory from the version control system:

1
2
git rm --cached /path/to/files
rm -rf /path/to/files

Again to modify .gitmodules , remove the unwrought submodule, for example:

1
2
3
4
5
6
[submodule ".vim/bundle/vim-gitgutter"]
path = .vim/bundle/vim-gitgutter
url = git://github.com/airblade/vim-gitgutter.git
-[submodule ".vim/bundle/vim-autoclose"]
- path = .vim/bundle/vim-autoclose
- url = git://github.com/Townk/vim-autoclose.git

Not yet finished! But also modify the contents of .git / config , with. Gitmodules , the need to remove the submodule deleted, and finally commit.

clone when the submodule together to catch down

Implementation of git cloneGit clone does not automatically come together when submodule must be added --recursiverecursive parameter, so you can catch all associated submodule in submodule down together:

1
git clone --recursive git@github.com:Chen-tao/go-stuff.git

If you have been caught to find submodule is empty, you can use the following instructions to catch, initwill be registered in _.git / config` remote repo URL and local path:

1
2
git submodule init
git submodule update --recursive

Or merged into one line git submodule update --init --recursivecan, if upstream someone to turn over .gitmodules , it seems that local end also using this method update.

Explanation of instructions

  • git submodule init: According to .gitmodules name and URL, these information will be registered to . *Git */ config , but not .gitmodules not removed submodule, the use of this directive and no way to automatically delete. Git / config related content, you must manually delete ;
  • git submodule update: According to registered (that is, .git / config ) submodule to update, such as clone lost submodule, which is the last paragraph of the method, so the implementation of this instruction before the best plus --init;
  • git submodule sync: If the submodule’s remote URL changes, you can correct the URL in .gitmodules , and then execute this command, the submodule’s remote URL will be corrected.