How to migrate and synchronize Hexo and theme-next with git submodules

The scenario is that I would like to migrate my hexo repo from my old laptop to the new desktop.

git push the right repo to github

git push the source repo(e.g. https://github.com/ZedYeung/Hexo) with such structure(image_path_replace.py is my own script)

1
2
3
4
5
6
7
8
9
10
├── _config.yml
├── db.json
├── image_path_replace.py
├── node_modules
├── package.json
├── package-lock.json
├── public
├── scaffolds
├── source
└── themes

rathan than the generated repo(e.g. https://github.com/ZedYeung/zedyeung.github.io)

1
2
3
4
5
6
7
8
9
10
11
12
13
├── 2017
├── archives
├── avatar.jpg
├── categories
├── CNAME
├── content.json
├── css
├── images
├── index.html
├── js
├── lib
├── node_modules
└── tags

The last one is generated by the former one with hexo-deployer-git

git submodule add hexo-theme-next

Problem

Assume that hexo source repo named Hexo
hexo-theme-next in Hexo/themes/next

run git config --get remote.origin.url in Hexo, we should get this

git@github.com:hexojs/hexo-starter.git

run git config --get remote.origin.url in Hexo/themes/next, we should get this

git@github.com:iissnan/hexo-theme-next.git

If we run git add . to add Hexo/themes/next, we would get the warning

1
2
3
4
5
6
7
8
9
10
11
12
13
14
warning: adding embedded git repository: themes/next
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> themes/next
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached themes/next
hint:
hint: See "git help submodule" for more information.

Why this happen

Let’s traceback how we get this. How we get this Hexo repo with hexo-theme-next

1
2
3
4
git clone git@github.com:hexojs/hexo-starter.git
cd hexo-starter/themes
git clone git@github.com:iissnan/hexo-theme-next.git
mv hexo-theme-next/ next

now we run git status,

1
2
3
4
5
6
7
8
9
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
(use "git add <file>..." to include in what will be committed)

next/

nothing added to commit but untracked files present (use "git add" to track)

Now if we run git add ., we would get this warning.
Because we have nested git repo! we git clone a repo in a git repo’s subfolder!

Solution

  1. Add the hexo-theme-next to one remote repo and push it

    1
    2
    3
    cd Hexo/themes/next
    git remote add some-name-rather-than-origin git@github.com:User/hexo-theme-next.git
    git push some-name-rather-than-origin master
  2. git submodule add hexo-theme-next(cd Hexo/) ,

    If we add it directly, we probably would see this
    'themes/next' already exists in the index

    so first run git rm -r --cached themes/next

    then add it git submodule add git@github.com:User/hexo-theme-next.git themes/next

    Run cat .gitmodules, we should see something like this

    1
    2
    3
    4
    5
    6
    [submodule "themes/landscape"]
    path = themes/landscape
    url = https://github.com/hexojs/hexo-theme-landscape.git
    [submodule "themes/next"]
    path = themes/next
    url = git@github.com:ZedYeung/hexo-theme-next.git
  3. Add the Hexo to another remote repo and push it

    1
    2
    3
    4
    git add .
    git commit -m "add: hexo-theme-next submodule"
    git remote add some-name-rather-than-origin git@github.com:User/Hexo.git
    git push some-name-rather-than-origin master

git clone to new computer

Requirement: npm install hexo-cli -g

1
2
3
4
git clone git@github.com:ZedYeung/Hexo.git
cd Hexo
git submodule init
git submodule update

[1] https://github.com/hexojs/hexo/issues/2914
[2] https://stackoverflow.com/questions/4089430/how-can-i-determine-the-url-that-a-local-git-repository-was-originally-cloned-fr
[3] https://git-scm.com/book/en/v2/Git-Tools-Submodules
[4] https://stackoverflow.com/questions/12218420/add-a-submodule-which-cant-be-removed-from-the-index
[5] https://stackoverflow.com/questions/26215625/name-option-doesnt-work-with-git-submodule-add-command