将本地存储库推送到远程存储库的新子目录

时间:2013-09-17 19:52:04

标签: git

我有一个我创建的本地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

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

1 个答案:

答案 0 :(得分:6)

在@twalberg的带领下,我找到了documentation for 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"