Reputation: 770
I was going to do more work on a feature and before coding i ran git pull --rebase upstream master
. Then ran git push origin feature-branch
; this resulted in one of those 'fast-forward' errors in which I then ran git pull origin feature-branch
. Following that I fixed a merge conflict and ran git push origin feature-branch
again. Now my pr from feature-branch
to master
is polluted with other's commits. I noticed this questions was asked previously but never answered.
Can someone explain what I did wrong and maybe how to fix it?
Upvotes: 14
Views: 11410
Reputation: 30408
If your current branch has commits you want to keep (your own) mixed in with commits you don't want (the ones added by the rebase), you can use git rebase --interactive
to edit your branch again and keep only the commits you pick.
First, find the hash of the commit before the last one you might want to edit. If your branch was originally based on master
, a good commit to choose is the one returned by git merge-base HEAD master
.
Next, start an interactive rebase from the current commit to that old commit:
git rebase --interactive f1c3d284
Your text editor will pop up with lines representing each commit in your branch and with instructions from Git below them. As the instructions say, change pick
to drop
for each line representing a commit you don't want. When you save and close the document, your current branch will be rebuilt according to those instructions.
Upvotes: 4
Reputation: 770
I figured it out:
Write down the git commit hashes for all commits in the PR that you want to save (i.e. your commits).
Then run the following:
git fetch upstream
git reset --hard upstream/master
git cherry-pick <hash 1>
git cherry-pick <hash 2>
// cherry-pick all of your commits then:
git push -f origin your-branch
And it should fix your PR automatically
Upvotes: 29