git – branching a branch from a given commit and migrating subsequent commits

Question:

I was updating a repository, creating its documentation, for that I created a new branch called master-guibook a "./docs" directory and a "book.json" file in the root of the project, then opened the Gitbook Editor.

But the Gitbook Editor thought he should go back to Branch Master on his own, and I didn't see him checkout to master, thus making some edits to the master.

I don't know how to migrate edits from a certain commit to the end of the current master to the `master-gitbook" branch

My tree currently looks like this:

master-gitbook /branch-master-gitbook-porem-vazio / master m1---m2---m3---m4---m5---m6---m7---m8---m9---m10

In this case, how should I make the commits from m4 to m10 to belong to the master-gitbook branch, so that I can continue my work?

And since I can't stop the work, assuming I have a new commit like below

master-gitbook /branch-master-gitbook-porem-vazio / master m1---m2---m3---m4---m5---m6---m7---m8---m9---m10---m11

make commits from m4 to m10 belonging to the new branch and keep m1 in the master branch make them look like this:

master-gitbook /---m4---m5---m6---m7---m8---m9---m10 / master m1---m2---m3---m11

Answer:

You can fast-forward from master-gitbook to m10 and then remove commits m4 to m10 from master . Use the commands below (replacing mX with the SHA-1 hash of the respective commit):

git checkout master-gitbook
git merge m10
git checkout master
git rebase -i m3

In the text editor that will open, delete all the lines referring to commits m4 to m10 . Only lines m11 onwards should be left.

Note: the git merge m10 command will only work as it wants if the master-gitbook branch is still on m3 . Otherwise a merge commit will be created instead of fast-forwarding or rebase should be used instead of merge .

Scroll to Top