简体   繁体   中英

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
      |
      v
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
        |
        v
        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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM