Let's say I have a repository with branch master. I checkout a branch feature-branch and started working on it. I raise a PR and push 3 commits in the PR over time. Let's say the master branch moves forward with 10 commits during this time and I have to rebase my PR to the tip of master. I follow these commands for that:
git checkout feature-branch
git rebase master
git pull
git push
These steps do put my PR forward but now my PR contains 3 + 10 = 13 commits, which makes getting it reviewed difficult. Any solution for this?
Ok.... as LightBender is saying, you have a problem in your recipe because you are merging the old branch that you are trying to rebase so you are keeping the old commits (not to mention that I am not seeing that you are pulling changes into your local master).
The way you should go about it normally is:
git checkout master
git pull # bring changes from upstream master into your local master
git checkout feature-branch
git rebase master # this is how you move your branch on top of master, keeping it straight
git push -f # assuming that the upstream branch as been set before
# if the upstream branch is not set, then write git push -f origin @ -u
# after that, you can just run git push -f (on subsequent pushes)
Now.... how do you get out of the mess that you are in? Well.... I am not completely sure this will work, but it will probably. Assuming that you just came out of the last pull that you were doing there:
git checkout feature-branch~ # go to the first parent of that merge.... you should be on top of 3 commits that are straight from main, no merges
# If that is the case, just place your branch over here:
git branch -f feature-branch
git checkout feature-branch
git push -f
If actually, that is not the case, then you will need to get yours hands dirty and cherry-pick the 3 revisions that you want to have in your branch.... or rebase interactive, which is nice, but can be a little bit scary... with cherry-pick it should be simple:
git checkout --detach master # let's get ourselves on top of master, but let's not move it around
git cherry-pick first-commit-of-the-branch-you-want #you probably have more than one _first_ commit in the messy branch, any of those _firsts_ would do, use its commit ID
git cherry-pick second-commit-of-the-branch-you-want # same thing with the second
git cherry-pick third-commit-of-the-branch-you-want
# if this is your branch and it looks good (straight line from master, 3 commits, no merges):
git branch -f feature-branch
git push -f
And you got out of hell. Now you can start using the recipe as I described in the beginning.