Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

How to merge my code with code coming from a different repository in git

one month ago i started working on a new function on one of our private repositories on github. I cloned the repository and branched it. I've been working on the branch ever since.

In the meantime the code was moved to a different repository on github. Now i need to merge what i've done with the new developments that have been going on in the new one.

My idea:

1 - checkout the new repository.

2 - Find the last common commit in my repository and the new one. This should be the last commit on the master branch.

3 - Create a patch between the master branch and my working branch.

4 - Apply the patch to the new repository.

Is this correct? What should be the git commands to do 3 and 4 ?

Upvotes: 1

Views: 167

Answers (2)

twalberg
twalberg

Reputation: 62379

If the new repository is truly a fork/clone of the old one, then you should be able to simply replace the URL in .git/config with the new URL, then git fetch to pull everything from the new project. Once that's done you can use git rebase or git merge as appropriate.

If, instead of tracking just one project, you want to track both projects (is there ongoing development in the old project that you might still want to pull in occasionally?), then you can just add the new repository as another remote - git remote add newproj <URL>. You could even rename the old one to something other than origin so you could use the origin name for the new repository.

That would avoid needing to go the patch route, as you'll really just be pointing your local repository at the new location, and will have direct access to all the new commits as well as the old ones.

Upvotes: 0

Stefan
Stefan

Reputation: 114178

git-rebase should do what you want, i.e. replay your changes onto the new repo:

git remote add new-repo /path/to/new-repo
git fetch new-repo
git checkout hacking-branch
git rebase hacking-branch new-repo/master

Upvotes: 1

Related Questions