{"thread":{"id":"43913","subject":"Possible bug: git pull --rebase discards local commits","startedAt":"2016-08-23T16:26:02Z","lastAt":"2017-03-10T00:23:33Z","messageCount":3,"participants":["Joshua Phillips","Junio C Hamano","Igor Djordjevic"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"299955","messageId":"1471969497.3553135.703756633.0F6CCC4C@webmail.messagingengine.com","threadId":"43913","inReplyTo":null,"subject":"Possible bug: git pull --rebase discards local commits","fromName":"Joshua Phillips","fromEmail":"jphillips@imap.cc","sentAt":"2016-08-23T16:24:57Z","receivedAt":"2016-08-23T16:26:02Z","isPatch":false,"sender":{"key":"jphillips@imap.cc","avatar":null},"body":"I've found a case where git pull --rebase discards commits in my branch\nif the remote-tracking branch was rewound (and the remote tracking\nbranch's reflog contains my branch's latest commit). This is due to\ngit-pull's usage of git merge-base --fork-point.\n\nOn one hand, this behaviour might be correct since the remote repository\nessentially removed that commit from master by 'reset --hard'. On the\nother hand, I was surprised that git pull --rebase discarded a commit in\nmy branch.\n\nTested on 1.9.1, 2.7.4 and 2.10-rc1.\n\nSteps to reproduce:\n\n    # Set up initial repository\n    git init source\n    cd source\n    git config receive.denyCurrentBranch no # make 'git push' work - not\n    relevant to bug\n    echo hello > test\n    git add test\n    git commit -m \"Initial commit.\"\n\n    # Clone repository, make and push two commits.\n    cd ..\n    git clone source clone\n    cd clone\n    echo greetings >> test\n    git commit -a -m \"This commit is rewritten.\"\n    echo something >> test\n    git commit -a -m \"This commit disappears.\"\n    git push origin master\n\n    # Discard the second and rewrite the first commit.\n    cd ../source\n    git reset --hard HEAD~ # remove \"This commit disappears\"\n    git commit --amend -m \"This commit is a rewrite.\"\n    cd ../clone\n\n    # Observe that \"This commit disappears\" is still in our branch but\n    is not in origin/master\n    git fetch && git log --graph master origin/master\n\n    # Now git pull --rebase gets confused because origin/master used to\n    point to \"This commit disappears\",\n    # so it assumes that that was the fork point for master, and \"This\n    commit disappears\" is discarded.\n    git pull --rebase\n    git log --graph # \"This commit disappears\" is completely gone!\n"},{"id":"299980","messageId":"xmqqvayruvwk.fsf@gitster.mtv.corp.google.com","threadId":"43913","inReplyTo":"1471969497.3553135.703756633.0F6CCC4C@webmail.messagingengine.com","subject":"Re: Possible bug: git pull --rebase discards local commits","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-08-23T19:28:59Z","receivedAt":"2016-08-23T19:30:01Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Joshua Phillips <jphillips@imap.cc> writes:\n\n> I've found a case where git pull --rebase discards commits in my branch\n> if the remote-tracking branch was rewound (and the remote tracking\n> branch's reflog contains my branch's latest commit). This is due to\n> git-pull's usage of git merge-base --fork-point.\n>\n> On one hand, this behaviour might be correct since the remote repository\n> essentially removed that commit from master by 'reset --hard'. On the\n> other hand, I was surprised that git pull --rebase discarded a commit in\n> my branch.\n\nYup, that sounds like a bad way to handle the situation.  After all,\nthe upstream may have first accepted your first attempt, and then\ndecided that it was premature and rewound it, expecting you to give\nan improved reroll.  But I also agree with you that it may be\ncorrect to drop it because the upstream already rejected it.\n\nSince Git cannot tell between these two cases, we should play safer\nthan what the current code does, I would think.\n\n"},{"id":"313728","messageId":"85fefe0a-fe0e-cf1b-78c3-a8e2e85f53c2@gmail.com","threadId":"43913","inReplyTo":"xmqqvayruvwk.fsf@gitster.mtv.corp.google.com","subject":"Re: Possible bug: git pull --rebase discards local commits","fromName":"Igor Djordjevic","fromEmail":"igor.d.djordjevic@gmail.com","sentAt":"2017-03-10T00:23:13Z","receivedAt":"2017-03-10T00:23:33Z","isPatch":false,"sender":{"key":"igor.d.djordjevic@gmail.com","avatar":null},"body":"Hi Junio,\n\nOn 23/08/2016 21:28, Junio C Hamano wrote:\n> Joshua Phillips <jphillips@imap.cc> writes:\n> > I've found a case where git pull --rebase discards commits in my branch\n> > if the remote-tracking branch was rewound (and the remote tracking\n> > branch's reflog contains my branch's latest commit). This is due to\n> > git-pull's usage of git merge-base --fork-point.\n> >\n> > On one hand, this behaviour might be correct since the remote repository\n> > essentially removed that commit from master by 'reset --hard'. On the\n> > other hand, I was surprised that git pull --rebase discarded a commit in\n> > my branch.\n> \n> Yup, that sounds like a bad way to handle the situation.  After all,\n> the upstream may have first accepted your first attempt, and then\n> decided that it was premature and rewound it, expecting you to give\n> an improved reroll.  But I also agree with you that it may be\n> correct to drop it because the upstream already rejected it.\n> \n> Since Git cannot tell between these two cases, we should play safer\n> than what the current code does, I would think.\n\nWere there any news in this regards so far? Would it be (more) \nsensible/safe to report the dropped commits, too? Something like:\n\n  Dropping: Commit B\n  Applying: Commit X\n  Applying: Commit Y\n\n... where \"Applying: *\" are standard \"git rebase\" output lines, and \n\"Dropping: *\" a newly proposed one (example graphs (1.1) to (1.3) \nshown below[1]).\n\nThat said, applied commits might even be considered pretty \nuninteresting here (as they`re kept/transferred over anyway), a noise \ndrowning what otherwise might be really important - the dropped/lost \nones...?\n\nIt does feel a bit scary learning that you may _silently_ lose \ncommits, especially as --fork-point is used by default for both \nvanilla `git rebase` and `git pull --rebase`.\n\nP.S. As a relatively new user, I actually just got aware of this \nbehavior from another, recently posted e-mail[2], having me \ninvestigate further, yet thought replying here might be better as it \ngot some attention already (adding author of that other e-mail, \nLaszlo Kiss, to Cc).\n\nRegards,\nBuga\n\n[1] Example graphs:\n\n  (1.1) ---A---B (master)\n                \\\n                 X---Y (topic)\n\n  (1.2)      C---D (master)\n            /\n        ---A---B\n                \\\n                 X---Y (topic)\n\n  (1.3)            X'---Y' (topic)\n                  /\n             C---D (master)\n            /\n        ---A\n\n  Note that I didn`t use \"origin/master\" but just \"master\" on purpose, \n  as the branch being tracked doesn`t have to be a remote one, making \n  the lost local commits confusion even greater.\n\n[2] https://public-inbox.org/git/CAO0LFki4PN8zz2xpoSpjTHJGS=NG_suQYR27EcmzEMiaCw9kuA@mail.gmail.com/\n"}]}