17

我有一个本地 git 存储库,我创建并使用了几个月。但是,这项工作现在将成为具有现有存储库的较大项目的一部分,因此我想将较大项目的服务器添加为远程服务器并将我的工作推送到该存储库的子目录中。

但是我相信这些幼稚的步骤会将我的本地存储库推到更大项目存储库的顶级目录中:

$ pwd
/home/yotommy/my-local-project
$ git remote add origin git://example.com/gitscn/larger-project.git
$ git push origin master ### goes to top-level directory, not desired!

我尝试指定一个(尚不存在的)子目录:

$ git remote add origin git://example.com/gitscm/larger-project.git/my/sub/dir
$ git push origin master
fatal: '/gitscm/larger-project.git/my/sub/dir' does not appear to be a git repository

我应该先将子目录添加到更大的项目仓库吗?还是有更好的方法?

4

1 回答 1

7

在@twalberg 的带领下,我找到了 subtree merging 的文档。这是问题中提出的案例的简明配方。

$ git clone git://example.com/gitscm/larger-project.git
$ git remote add subproj_remote /home/yotommy/my-local-project
$ git fetch subproj_remote
warning: no common commits
remote: Counting objects: 461, done.
remote: Compressing objects: 100% (332/332), done.
remote: Total 461 (delta 157), reused 0 (delta 0)
Receiving objects: 100% (461/461), 272.89 KiB, done.
Resolving deltas: 100% (157/157), done.
From subproj_remote /home/yotommy/my-local-project
 * [new branch]      master     -> subproj_remote/master
$ git checkout -b subproj_branch subproj_remote/master
Branch subproj_branch set up to track remote branch master from subproj_remote.
Switched to a new branch 'subproj_branch'
$ git checkout master
Switched to branch 'master'
$ git read-tree --prefix=subproj/ -u subproj_branch
$ git commit -m "merged subproj"
于 2013-09-17T22:49:07.110 回答