I have two GIT repositories, one for work and one for independent development. My company expressed interest in a project I've been working on and would like me to migrate that branch (master) into a new branch on their repo (Features/Dynamo). I have created a migrator in the past but this clobbers the repo being pushed to as it is a straight --bare then --mirror. What would I need to do in order to graft a branch from one repository to another (while creating the new branch in the process)?
-
Are there any shared files (same dir/filenames) ? – Michael Durrant Apr 23 '15 at 21:17
-
Can you just add the other repo as another remote and push your master from A to features/Dynamo on B? – scrowler Apr 23 '15 at 21:17
-
And will this be a one-time transfer ? – Michael Durrant Apr 23 '15 at 21:17
-
This doesn't make sense? Why would you have two completely different projects in the same git repository as branches? Why not have two git repositories? – Paul Oliver Apr 23 '15 at 21:17
-
There are two git repositories, I would like to graft a branch from one repository to another, they want to use a wrapper that I wrote. – ehime Apr 23 '15 at 21:25
-
@MichaelDurrant yes this will be a one time transfer. Scowler, yes I could do that, I'm not positive how though. – ehime Apr 23 '15 at 21:26
3 Answers
Another approach is to add the other remote.
git remote add ehime https://github.com/your_username/your_repo_name.git
Now all the stuff you've done like
git push origin some_branch
can also be done as
git push ehime some_branch
for the other remote.
Thus two remotes but one directory of code.
In the case of shared files there are lots of options when doing a merge such as
git checkout branchA
git merge ours branchB
or
git checkout branchA
git merge -X theirs branchB
to control how merges are done between the two codebases.

- 93,410
- 97
- 333
- 497
A simple approach would be to get a clone of the two repos on one machine, then in the "to" repo git checkout -b new_branch
and then literally cp
the files in from the other projects directory. git add and git commit and you are done. As I asked above, whether this would work would depend on if you have the same dir/filenames. If you do you'd be presented with merge conflicts and the two different files would be smooshed together in a pretty bad way.

- 93,410
- 97
- 333
- 497
In theory, this is what you could do, but I think you'd be better served having two different repositories. NOTE: This will not bring over your entire git history, it will just add the most recent state of your project as a branch.
$ git checkout -b OtherProject
-- to prevent losing your git database
$ mv .git/ /temp/gitBackup
-- clear out all the stuff from the main repository
$ rm -rf *
-- copy contents of the tip of master from your other project
-- but not the .git/ folder
$ cp -r /path/to/personal/project/* .
$ rm -rf .git/
-- bring back your old .git folder
$ mv /temp/gitBackup ./.git
-- add all your new files to this branch
$ git add -A .
$ git commit -am "The other project is now in this repo as a branch"
Again, this is not a recommended course of action. Your git database will grow much larger and this is not the intended use for branches. I would highly recommend using one repository per project and perhaps using submodules to manage sub projects from within your git repository.

- 7,531
- 5
- 31
- 34