views:

60

answers:

2

Let's say I have two different repositories like so:

Project 1:
Init---A---B---C---HEAD1

Project 2:
Init---D---E---F---G---HEAD2

Is there a way to rebase Project 1 (Init to HEAD) to the Init commit of Project 2 so it looks like this:

Project 1 & 2:
     A---B---C---HEAD1
   /
Init---D---E---F---G---HEAD2

The content of Project 1 & Project 2 are similar. The main difference is that their file structure is slightly different like so:

Project1:

MyProject/
    File1
    File2
    File3

Project2:

MyParentProject/
    MyProject/
        File1
        File2
        File3
    SomeFolder/
    SomeOtherFolder/
    ...etc/

FYI: MyProject is not a submodule of MyParentProject. MyProject and MyParentProject exist in two separate locations as two separate git repositories.

A: 

I think you could use git-filter-branch with the --parent-filter option.

Stuart
+3  A: 

You could treat one as a remote repository to the other. In Project1, run these commands:

git remote add project2 <path_to_project_2>
git fetch project2
git branch --track project2Branch project2/master
git checkout project2Branch

Use git log to find the hash for the initial commit of that branch (which is Project2). Then run

git checkout master # or whatever branch you want to rebase
git rebase <hash-for-commit>

You've now rebased Project1 with Project2. Since this sounds like a one time operation, where you'll then only be using the one repository, you can cleanup with

git remote rm project2

So now your master branch is rebased with the init of Project2, and project2Branch has the rest of the history of Project2. It's sort of a hack, but it will do what you want.

bobDevil
`git fetch <path_to_project_2> master:project2Branch` will fetch the other project without making a temporary remote.
Chris Johnsen
This does work to some extent, but unfortunately some errors do occur because the file structures do not match.For example, lets say I modified File1 in Project 1, because File1 does not exist in the root folder of Project 2 git fails to appropriately merge the changes.
dirtytofu