缘起: 做一个小程序, 制作的过程中, 发现小程序官方有同步简历版本库的服务. 但是, 他指定的是我的版本控制中的一个子目录. 因此我要给这个子目录转化为一个可以独立提交的git仓库.

git subtree add --prefix=example-submodule https://github.com/githubtraining/example-submodule master --squash
  • example-submodule就是要单独成为库的子目录.
  • –squash的目的是这个库抽取有关提交, 如果不用这个参数, 那么所有提交都保存在新建的库里面, 但是, 这个库里面就会有很多空提交.

如果这个新库有自身的独立更新需要拉取

#拉取
git subtree pull --prefix=example-submodule https://github.com/githubtraining/example-submodule master --squash

#设置remote可以检查上面的操作
git remote add sub-remote https://github.com/githubtraining/example-submodule.git

推送到这个新库

git subtree push --prefix=example-submodule https://github.com/githubtraining/example-submodule master

此时, 我陷入了困境, 想找更完整的官方文档, 然后发现这些官网都不全:

  • https://git-scm.com/doc
  • https://github.github.com/training-kit/downloads/submodule-vs-subtree-cheat-sheet/
  • 这里才是:
  • https://git.kernel.org/pub/scm/git/git.git/plain/contrib/subtree/git-subtree.txt
  • 这个格式看起来舒服点: https://github.com/apenwarr/git-subtree/blob/master/git-subtree.txt
万能的so
  • https://stackoverflow.com/questions/37241996/how-to-handle-nested-git-repositories
  • 这里的零分答案很有意义
#子目录用的源
git remote add weborigin https://path.to/remotes/plugin.git
#给子目录加源, 成为subtree
git subtree add --prefix=web weborigin master
#pull/commit/checkout/... 这个源 
git subtree pull --prefix=web weborigin master
git commit -am "new changes to be backported"
git checkout -b backport weborigin/master
git cherry-pick -x --strategy=subtree master
git push weborigin backport:master

关于merge的相关内容

  • https://mirrors.edge.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html