Rebasing a branch that has been branched from

I’m attempting to cut off a part of the history of a repository so I
can graft it back onto the repository with git replace as described
in this article.

The main difference between the case described in the article and my
repository is, that my master branch has existing branches. I would
like to keep the layout of those branches and their branch-points
equivalent after I have cut away the history.

My history approximately looks like this

    cut here
o--o--o--o--o--o--o master
            \  \  \
             \  \  o--o   A
              \  o--o--o  B
               o--o--o    C

There are no branches off of master before the “cut here” commit.

What I want to achieve is this:

o--o--o history

initial tree at cut
        o--o--o--o--o master
               \  \  \
                \  \  o--o   A
                 \  o--o--o  B
                  o--o--o    C

That means it wouldn’t be sufficient to just rebase master on the
initial tree but I also need to replay all existing branch on their
equivalent new commit after the rebase of master. I don’t want to just
replay all branches on the new master, because that would possibly
have conflicts.

Is there any smart and hopefully automatic way to do this?

  • Using Git and Dropbox together effectively?
  • Git checkout <commit> and then commit and retain all commits
  • git pull currently tracked branch
  • Removing features from releases in Git Flow
  • What are the best tools for Sql Server version control
  • How can I quickly scroll down the output of git commands?
  • Popularity of Git/Mercurial/Bazaar vs. which to recommend
  • Development and testing branch in @nvie's Git branching model?
  • One Solution collect form web for “Rebasing a branch that has been branched from”

    These commands will have the result you are looking for:

    git checkout --orphan temp-branch CUT–COMMIT
    git commit // This will create your new initial commit
    git rev-parse HEAD // To get the SHA of this commit
    git replace CUT-COMMIT NEW-COMMIT
    git filter-branch -- --all

    If you have any question regarding to how and why this works, feel tree to leave a comment. You might want to backup your repo before you do stuff like this.

    From the filter-branch docu:

    NOTE: This command honors .git/info/grafts and .git/refs/replace/. If
    you have any grafts or replacement refs defined, running this command
    will make them permanent.

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