在一个 Git 仓库中聚合两个项目

主要操作 现在我们有两个仓库,servicecomp 和 srvc-frontend,我想把他们在一个仓库中聚合一下(比如前后端项目),使用 subtree # 0) 若父仓库已误把两个子仓库当普通目录 add 了,先从索引移除(不删本地文件) git rm --cached -r servicecomp srvc-frontend || true # 1) 临时挪开本地已有目录(subtree 会自己创建对应目录) mv servicecomp servicecomp.tmp mv srvc-frontend srvc-frontend.tmp 两个子项目的远端 URL 加进来,起两个新名字 # 2) 后端 servicecomp git remote add servicecomp-origin [email protected]:Ch1ldKing/HITCS-CloudNative-SpringCloud.git git fetch servicecomp-origin git subtree add --prefix=servicecomp servicecomp-origin main -m "Add servicecomp via subtree" # 暂存更改 git stash push -u -m "wip before adding srvc-frontend" # 3) 前端 srvc-frontend(URL 和分支名请替换) git remote add srvc-frontend-origin <你的前端仓库URL> git fetch srvc-frontend-origin git subtree add --prefix=srvc-frontend srvc-frontend-origin <分支名> -m "Add srvc-frontend via subtree" # 改动取回来 git stash pop 错误 错误 1 $ git subtree add --prefix=servicecomp servicecomp-origin main -m "Add servicecomp via subtree" fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' fatal: working tree has modifications. Cannot add. fatal: ambiguous argument ‘HEAD’ ...

2025年10月15日 · 1 分钟 · 201 字 · Dorianyang