I have the following structure in my repo:
| |-commit9 [bugfix]
| |-commit5 [branch-2]
| |-commit2 [branch-1]
Well you're getting there.
First of all, push to a remote or clone your local repo in another place. Just to be sure. After that lets fix that master branch. Be sure you're on that branch before we continue.
git checkout master
Now we don't want those other commit, just 8 and 9. Since they're already there we can simply reset the master branch to commit 9 and remove the other commits. No worries, your commits are still in the branches so nothing should be lost.
[commit9@master] with the sha of your commit.
git reset --hard [commit9@master]
Now go ahead and checkout the bugfix branch and reset it to commit 7 the same way as we did with the master branch.
git checkout bugfix git reset --hard [commit7@bugfix]
Now for the last part to get back to a linear flow you can merge your hotfix back to master and have all your work at the same place.
git checkout master git merge bugfix
I guess the easiest way would have been to checkout your master branch. Cherry-pick the two commits you wanted to move. Go back to your original branch and reset it back two commits. The latter can be done by using
git reset --hard HEAD^2
Which means remove two commits from my current position.
Little warning: Moving commits can be dangerous. Since you made your commits based on a certain change set, the same code might not behave the same way in its new position. Seeing your moved code might depend on code no longer existing.