Use Git Submodule to Manage Project Dependencies

It’s common to include a third-party git repo in your own git project. Sometime I want to make some local changes to the third-party repo without pushing to the origin master and also want to keep other parts in sync with the third-party’s master. This article provides one way to achieve this by using git submodule.

I’m using adding a theme to hexo as an example, I have a git repo of a hexo project:

gen@do:~/hexo$ tree -L 1 -a
. <my parent git repo>
├── _config.yml
├── db.json
├── .git
├── .gitignore
├── node_modules
├── package.json
├── public
├── scaffolds
├── source
└── themes
    └── <theme's git repo put here>

I want to add this theme( by putting its git repo into the themes directory.

Step 1: Fork the theme’s repo

It is as easy as going to and click the fork button. Now I have my fork of the repo at

Step 2: Add submodule

gen@do:~/hexo$ git submodule add themes/modernist

This works like git clone, now the themes dir look like:

gen@do:~/hexo$ tree themes/ -L 2
└── modernist
    ├── _config.yml
    ├── languages
    ├── layout
    ├── LICENSE
    └── source

Also a .gitmodule file is created to trace the submodule:

gen@do:~/hexo$ cat .gitmodules
[submodule "themes/modernist2"]
    path = themes/modernist2
    url =

Step 3: Add the original repo as upstream

gen@do:~/hexo/themes/modernist2$ git remote add upstream
gen@do:~/hexo/themes/modernist2$ git remote -v
origin (fetch)
origin (push)
upstream (fetch)
upstream (push)

Step 4: Make local changes to the theme(submodule) and commit

Step 5: Sync original repo

gen@do:~/hexo/themes/modernist2$ git fetch upstream
gen@do:~/hexo/themes/modernist2$ git checkout master
gen@do:~/hexo/themes/modernist2$ git merge upstream/master


Initialize submodule in newly cloned repo

When I clone the parent(hexo) repo somewhere else, the submodule(themes/modernist) won’t automatically cloned. I only get a empty dir of themes/modernist. Need to initialze it:

gen@do:~/hexo2 $ git submodule init
gen@do:~/hexo2 $ git submodule update

Delete submodule

gen@do:~/hexo2 $ git submodule deinit themes/modernist
gen@do:~/hexo2 $ git rm themes/modernist