Search code examples
gitclone

Git - clone another project branch to current project


I have two remote projects; proj1 and proj2

I have created a local repository of proj2

git init
git remote add proj2 <url>
git fetch --all --prune

This gives me two branches - master and develop. I change to develop

git checkout develop

with the template we use I now have a number of empty project directories. However I want to remove these and copy over the contents from the develop branch of proj1.

I've tried using clone but to no effect.

Andrew


Solution

  • You can add multiple remotes and fetch from them:

    git remote add proj2 <url>
    git remote add proj1 <url>
    git fetch --all
    

    You now have both proj1 and proj2 in your local repo, including references to the remote branches (proj1/develop, proj1/master, proj2/develop, proj2/master, etc..) You can merge, cherry-pick, or any other git operation between these branches to do what you need. You can selectively push your changes to proj1 or proj2 or both.

    git checkout develop
    git merge proj1/develop
    git merge proj2/develop
    <make more changes and resolve conflicts>
    git commit -a
    git push proj2 develop