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

瘦欲@ 提交于 2020-12-30 07:59:09

问题


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?


回答1:


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.




回答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:

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



来源:https://stackoverflow.com/questions/37673813/how-to-rebase-commits-from-another-repository-with-a-different-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!