Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push local repo to new sub-directory of remote repo

Tags:

git

I have a local git repository that I created and have been using for months. However this work will now be part of a larger project that has an existing repository, so I'd like to add the larger project's server as a remote and push my work into a sub-directory of that repository.

However I believe these naive steps will push my local repo into the top-level directory of the larger project's repo:

$ 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!

I tried specifying a (not-yet-existing) subdirectory:

$ 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

Should I add the sub-directory to the larger-project repo first? Or is there a better approach?

like image 491
yotommy Avatar asked Sep 17 '13 19:09

yotommy


People also ask

How do I push a subfolder in GitHub?

move everything from the subdirectory of the parent repo work tree to the child repo work tree. commit the child repo. replace the subdirectory in the parent repo with a submodule reference.

How do I push a local repo to an existing remote?

You can use the command git remote to do this. Then you can do your first push. With simple command git push . After that you can do all remote operation like fetch,pull and push.


1 Answers

Following the lead from @twalberg, I found documentation for subtree merging. Here is a condensed recipe for the case posed in the question.

$ 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"
like image 175
yotommy Avatar answered Sep 22 '22 14:09

yotommy