0

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
Animeta
  • 1,241
  • 3
  • 16
  • 30
  • I think it is better if you show us this output after all those operations: `git log --oneline --graph test-rebase origin/test-rebase main origin/main` – eftshift0 Jan 16 '23 at 09:15
  • @eftshift0 I have updated with your advice, these commits with message like 'message-*' are other's commits. I don't know why they have new commit hash in branch test-rebase. – Animeta Jan 16 '23 at 09:26
  • I provided the explanation about what is going on as an answer already. – eftshift0 Jan 16 '23 at 09:34

1 Answers1

1

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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Can you give me more detail about that `git pull -r`, I'm still a little confused about that, but `git push -f` do solve my problem, thanks! – Animeta Jan 16 '23 at 10:10
  • 1
    Well, `git pull -r` tells git: _do a fetch to get the latest updates from the upstream branch... then do a rebase of current branch on top of its upstream_. So, assuming that you are the only one pushing into that branch, the upstream is _behind_... as expected, because you pushed into it before you rebased.... but then rebase checks what commits are between the upstream and your branch... and it's the last 4 commits of `main` and then your final commit... and they will _all_ be rebased on top of the original commit that you did on your branch (the current position of upstream).... BOOM!!!! – eftshift0 Jan 16 '23 at 10:13