git rebase with branches (whole tree)

I am trying to connect two independent commit trees into one repository.

Initially I have:

  • Capistrano deploy from multiple git repository
  • How to input password to git push from script without using SSH keys?
  • Case insensitive completion for git in zsh
  • Submitting multiple pull requests in Git with Github (General flow)
  • How to change a git repository cloned with --bare to match one cloned with --mirror?
  • How to ask git if the repository is in a conflict stage?
  • A--B--C--D (master1)
    
    E--F--G--H (master2)
       \
        I--J (somebranch)
    

    These tree have no common ancestor and are independent of each other (different files and directories).
    I want to rebase both master2 and somebranch (may be multiple branches in fact) onto master1.

    It’s easy to rebase just single branch with:

    #simple reabse:
    git checkout master2
    git git rebase master1
    

    which would give me:

    A--B--C--D (master1)
              \
               E'--F'--G'--H' (master2)
    E--F--G--H
       \
        I--J (somebranch)
    

    Preferably I would like a single command to move both (or more) branches simultanously, to get result like this:

    A--B--C--D (master1)
              \
               E'--F'--G'--H' (master2)
                    \
                     I--J (somebranch)
    

  • How do I delete the current git remote add origin link I typed?
  • Is git good with binary files?
  • Unable to perform git-svn clone on a sub-repository requiring AD authentication
  • How to create a release branch in GIT?
  • Cloning git repository Failure
  • indentation configuration only for some files
  • 2 Solutions collect form web for “git rebase with branches (whole tree)”

    You can use a lesser-known feature called grafts to splice the histories together.

    $ echo `git rev-list [E] -1` `git rev-list master1 -1` > .git/info/grafts
    $ git filter-branch --tag-name-filter cat master2 [other branches]
    

    Or if there are multiple branches involved, and you don’t want to have to list them all, you can do:

    $ git filter-branch --tag-name-filter cat `git show-ref --heads | awk '{print $2}'`
    

    (for more details, see answer 161928, or blog article or advanced instructions)

    I have answered this question here and here. The short answer, use rebase with the --preserve-merges option, but before join all branches into a common, artificial node with:

    git checkout H
    git merge -s recursive -Xours J # will generate a J2 node
    

    The rebase:

    git rebase --preserve-merges --onto D E^ J2
    

    Where E^ is the parent of E and J2 the resulting node of the merge. If you use E instead of E^, node E won’t be rebased.

    See more detailed comments in the quoted posts.

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