I have searched for similar question like Accidentally added other's commits to my PR using git rebase. The answer for that just provide a method to remove unwanted commit from pr, but I still don't know why this happen.
These are commands to reproduce my problem, but I'm not sure for others.
>>> (main) git log --oneline --graph -5
* fa9a315
* e52240c
* 25c40fb
* 9ceb4b0
* ed40e2c
>>> (main) git checkout ed40e2c
>>> (@ed40e2c) git switch -c test-rebase
>>> (test-rebase) echo "foo" >> ./test.txt
>>> (test-rebase) git commit -am "test message"
>>> (test-rebase) git push -u origin test-rebase
>>> (test-rebase) git branch -vv | grep test-rebase
* test-rebase 6c0f33e [origin/test-rebase] test message
>>> (test-rebase) // create a pr in GitHub
>>> (test-rebase) git rebase main
>>> (test-rebase ⇣1⇡5) git pull -r
>>> (test-rebase ⇡4) git push
>>> (test-rebase) // now my pr has other's commits, and I'm also the coauthor of those commits
>>> (test-rebase) git log --oneline --graph test-rebase origin/test-rebase main origin/main
* 8bd227f (HEAD -> test-rebase, origin/test-rebase) message-4
* 938935c message-3
* 4080d9d message-2
* 8dbd5cd message-1
* 6c0f33e test message
| * fa9a315 (origin/main, origin/HEAD, main) message-4
| * e52240c message-3
| * 25c40fb message-2
| * 9ceb4b0 message-1
|/
* ed40e2c
CodePudding user response:
I think I understand what is going on.
So.... first, you push the branch after adding a single commit, right? And you set it as the upstream. So, you have this:
XXXX test message (HEAD -> test-rebase, origin/test-rebase)
ed40e2c whatever commit on that commit
Then, you rebase on top of main
.... so you end up with:
* XXXX' test message (HEAD-> test-rebase)
* fa9a315 blahblah (main)
* e52240c blahblah
* 25c40fb blahblah
* 9ceb4b0 blahblah
| * XXXX test message (origin/test-rebase)
|/
* ed40e2c blahblah
Then you run git pull -r
and this is where you mess up because you are rebasing the commits that are coming from main
that are already part of your branch on top of the branch that you pushed already. That's why your PR includes those commits you are talking about.
I think instead of git pull -r
you should do git push -f
so that you put your just rebased on top of main
branch replacing the old branch for your PR.