Cloning a GIT branch to another repositories GIT branch?

361 views Asked by At

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)?

3

There are 3 answers

0
Michael Durrant On BEST ANSWER

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.

0
Michael Durrant On

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.

0
Paul Oliver On

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.