A cheatsheet of the git submodule command

Owning to needing to change the theme of hexo, I need a way to modify them with git submodule. Here is a quick cheatsheet for myself.

Why git submodule

One of the means to maintain a repository in another repository.

Cheatsheet

To add a repository:

git submodule add <repository>

To add a repository with a specific directory folder

git submodule add <repository> <direcotry>

To modify the code for a submodule

1
2
3
4
5
6
$ cd submodule-folder
$ vim file-to-be-modified
$ git commit -am "foo"
$ git push origin a-remote-branch
$ cd root-folder
$ git commit -am "update"

To pull the latest commits for a submodule

1
$ git submodule update remote --merge

Specifying which branch to pull for a submodule

Open .gitmodules and add the branch field to the config

1
2
3
4
[submodule "themes/landscape"]
path = themes/landscape
url = https://github.com/zushenyan/hexo-theme-landscape.git
branch = andrew

Links

How to resolve timeout when cloning mega-sized repository with git

1
2
> git clone url-to-a-repository
error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

So, recently I was trying to clone a mega-sized repository while working on a company project. However, whenever I wanted to clone it with git clone it kept popping up the error above. It looks like the root cause is that there is a buffer limit when I need to pull it with HTTPS.

The solution to this is to

  1. Use SSH to clone the repository.
  2. Use HTTPS but pull the content of the repository incrementally.

Due to being unable to resolve SSH credential in a short time frame, I chose the second approach

1
2
3
> git clone xxx --depth 1
> cd xxx
> git fetch --unshallow

With the commands above, the content can be pulled part by part, instead of in one big chunk.

If it still errors with the same message, instead of git fetch --unshallow, try using the following command:

1
2
3
4
> git fetch --depth=100
> git fetch --depth=200
> git fetch --depth=300
# until the content has been fully pulled