How to rebase one repo to another

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

Project 1:

Project 2:

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

  • git merge Already up to date when it is not
  • setting the work-tree of each bare repo
  • Is it possible to have git encrypt and decrypt a file during a push/pull?
  • How to change the behavior of git status showing modified files which are eventually not committed?
  • Git: reset --hard now I'm stuck in the past
  • How to clone a Github Gist via SSH protocol?
  • Project 1 & 2:

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


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

  • How to get mongrel working with bundler?
  • trying to execute git command using python script
  • git push --recurse-submodules=on-demand is not truly recursive
  • Can I move a local Git repository to elsewhere within my hard drive safely?
  • Git clone fails with out of memory error - “fatal: out of memory, malloc failed (tried to allocate 905574791 bytes) / fatal: index-pack failed”
  • How do we connect to TFS from all Git repositories in a Team Project?
  • 4 Solutions collect form web for “How to rebase one repo to another”

    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.

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

    The solution I used was as follows:

    • In Project 2:

      git format-patch <commit> --stdout > /path/to/patch.diff

      Where <commit> refers to the first commit in the “from” repository you wish to merge to the “target”.

    • In Project 1:

      git am /path/to/patch.diff

    This replays every commit from <commit> to HEAD in Project 2 onto Project 1.

    This completely avoids the need for a common ancestor between the projects, as most git-specific tools require.

    You can add the other repo to your current repo first as a remote and then do a rebase –onto to rebase a range of commits from one repo to a point of common commit in the first repo.

    git remote add project2 <...>
    git checkout project2-master
    git rebase -s recursive -X theirs --onto \
        project1-first-commit project2- start-commit project2-end-commit

    Roughly like that. Not that I also specify merge strategy to use the commits from project2 if there are conflicts. You may want to use something else. But here we assume that project2 is “correct” code and project1 is just a remote master.

    Git Baby is a git and github fan, let's start git clone.