We have two Git repositories.
Repo 1. Commits: A, B, C. This repository was created from SVN history.
Repo 2. Commits: D, E, F. This repository was created without SVN history, just by using the working copy (as of commit C) which became the commit D. In another words, the file trees of the commits C und D are the same.
Now, we want to merge both repositories so we have the full history in one repository. Is there a way to "copy/rebase/something else" all the commits E..F onto C?
Changing older or multiple commits. To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.
Instead of blindly moving all of the commits to the new base, interactive rebasing gives you the opportunity to alter individual commits in the process. This lets you clean up history by removing, splitting, and altering an existing series of commits. It's like Git commit --amend on steroids.
The easiest thing to do would be to add Repo 2 as a remote in Repo 1 using its physical path on disk and then simply cherry pick commits E
and F
on top of C
.
So, in Repo 1 you would do:
git remote add repo2 C:\Path\To\Repo2
git fetch repo2
then, assuming E
and F
are the latest two commits in the master
branch in Repo 2 and C
is the latest commit in master
in Repo 1, like this:
master
/
A-B-C
repo2/master
/
D-E-F
you would do:
git checkout master
git cherry-pick repo2/master~2..repo2/master
which will apply the patches from commits E
and F
on top of C
:
master
/
A-B-C-E'-F'
repo2/master
/
D-E-F
Note that here I'm cherry-picking a range of commits, which is supported since Git 1.7.2.
Are these remote repositories? If both C and F are on the same local repository then you should be able to rebase the branch pointing to F onto the branch pointing to C. If not try this:
git remote add repo2 <Repo 2 URL>
git push repo2 branchPointingToC
git fetch
git checkout branchPointingToF
git rebase -i remotes/origin/branchPointingToC
(N.B. If instead you followed Enrico's answer but wanted to rebase you would use git rebase -i repo2/master
in place of his git cherry-pick
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With