Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebase commits from another repository with a different history?

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?

like image 800
Igor Mukhin Avatar asked Jun 07 '16 08:06

Igor Mukhin


People also ask

Does rebase change history?

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.

How does rebasing affect the history of a repository?

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.


2 Answers

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.

like image 89
Enrico Campidoglio Avatar answered Sep 20 '22 02:09

Enrico Campidoglio


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:

  1. Locally in Repo1 run
    a. git remote add repo2 <Repo 2 URL>
    b. git push repo2 branchPointingToC
  2. Then locally in Repo2 run
    a. git fetch
    b. git checkout branchPointingToF
    c. 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.)

like image 25
dumbledad Avatar answered Sep 19 '22 02:09

dumbledad