I have a repo with a master branch of 4 commits, then I created a new branch (b2) and made the fifth commit.

after I merged b2 to master. $ git merge b2 and I got :

enter image description here

Now I wanna change the order of commit 2 so I made ($ git rebase -i HEAD~5).

Then I got this result :

enter image description here

and the result I want is : 1-3-4-2-5(HEAD → master, b2)

1 Answer 1


When you rebase, the commits that you reorder change. That is because inside the commit there is a reference to the parent commit. If you reorder them, the parent commits change, so the references change and as a result, the commit themselves change. You can see this in your log, because the hashes (the 7-character codes in blue) changes for commit 4.

All downstream commits change as well, because their parent is now different. As a result, all commits downstream (more recent) change.

Your branch b2 points to a specific commit, namely 87c3249. When you rebase master, master points to a new commit (7812364), but that doesn't change b2.

If you want to fix this, run git branch -f b2 when master is checked out. It creates a new branch at the same position as master with the name b2, and forces it (-f), meaning you remove the existing branch first.

This is only a viable option if b2 and master point to the same commit. If b2 is ahead of master, you should probably create a new branch at master (let's say b3), and then cherry-pick the commits from the b2 branch that are ahead of master.

git checkout master
git tag old-master  # tag for later reference
git rebase -i  # rebase master
git branch b3  # create new branch at the same commit as master
git checkout b3
git cherry-pick old-master...b2  # cherry-pick the commits between the old master and b2

Now b3 should be ahead of master with the same commits b2 used to be ahead of master. You can now remove b2 and rename b3 to b2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.