Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

amending PR branches via force-push causes unreachable commits to be garbage-collected #997

Open

Description

@aspiers

When you want to amend a pull request, you have a choice of two ways to do it:

  1. Push new commits on top of the head of the existing PR branch
  2. Force-push (git push -f) a new head to the PR branch, thereby rewriting history.

Unfortunately currently both have significant problems, as explained in blog posts by @jd, myself, and probably others. @jd subsequently blogged that the improvements GitHub made more recently are sufficiently good for him, but the problems associated with rewriting PR history which he originally pointed out still remain.

This issue is for tracking just one of those problems: when you rewrite PR history, there is no guarantee that commits from older versions of the PR will be preserved, unless they are still reachable from the PR branch head. (See #998 and #999 for other, separate problems with force-pushing to PR branches.)

If these older commits are no longer reachable, for some period of time it will be possible to view them by constructing the correct https://github.com/USER/REPO/commit/SHA1 URL, or recover them from GitHub's "reflog" equivalent, but eventually they will be garbage-collected at some point by GitHub's backend.

This is in stark contrast to Gerrit which preserves every version of a patch set for every review.

Since we can't easily trigger GitHub's garbage collection, it's hard to definitively prove this is a genuine issue. However there is some strong evidence:

I was also attempting to prove it via an experiment in aspiers/test#3 - I previously thought that if within a few months of writing this, the test commit was still viewable, then maybe this was not genuine issue. But at the time of writing (editing) this text, the commit was still visible, so bearing in mind the above evidence, it's more likely that there is something else going on here. For example, maybe my attempt to construct an obfuscated URL which didn't accidentally prevent the commit from being garbage-collected didn't work. Or maybe the garbage-collection is triggered not by the age of orphans, but by the number of orphans, or when the number of objects exceeds a threshold, in which case my test repository will be useless in this experiment since it has almost zero traffic. GitHub might even be using git-gc(1) behind the scenes.

Activity

  1. jd commented on Aug 17, 2017

    @jd

    I think your analysis is right. We could probably enhance https://github.com/jd/git-pull-request to push the old ref to a random tag so it's not garbage collected though.

    I've opened an issue at https://github.com/jd/git-pull-request/issues/15 just in case it's feasible and interesting.

  2. aspiers commented on Aug 17, 2017

    @aspiers
    CollaboratorAuthor

    @jd Thanks! Your review of #998 and #999 is also very welcome ;-)

  3. aspiers commented on Sep 19, 2017

    @aspiers
    CollaboratorAuthor

    I'm also attempting to prove it via an experiment in aspiers/test#3 - if within a few months of writing this, the test commit is still viewable, then maybe I was wrong after all, in which case this issue can be closed.

    Actually, the garbage-collection could be triggered not by the age of orphans, but by the number of orphans, in which case my test repository will be useless in this experiment since it has almost zero traffic.

  4. aspiers commented on Sep 19, 2017

    @aspiers
    CollaboratorAuthor

    A corollary of this issue is that any comments on those garbage-collected commits would also presumably be lost forever - see isaacs/github#443 (Don't lose commit-related comments on rebase).

  5. aspiers commented on Oct 17, 2017

    @aspiers
    CollaboratorAuthor

    I've just remembered that GitHub's UI itself explicitly admits this issue. See how after I clicked on the icon showing my review of a previous version of a PR, it presented this screen which says "Sometimes commits can disappear after a force-push":

    gc-gc

  6. aspiers commented on Apr 30, 2018

    @aspiers
    CollaboratorAuthor

    @cirosantilli Please could the pull-requests label (and maybe also the code review label) be added to this?

  7. carlescufi commented on Jun 29, 2018

    @carlescufi

    +1 for this one, our project uses a rebase-driven PR approach as well. Supporting push -f properly so that rebased PRs display their history would be a major help for us.

  8. donquixote commented on Jun 29, 2018

    @donquixote

    We could probably enhance jd/git-pull-request to push the old ref to a random tag so it's not garbage collected though.

    As @jd says: Tags can be a solution.

    I think there needs to be a manual way to delete such tags, if you truly need to delete a commit e.g. for privacy reasons.

  9. aspiers commented on Jun 29, 2018

    @aspiers
    CollaboratorAuthor

    Agreed. BTW it's easy to delete tags.

    Also worth noting that GitLab correctly handles this and also doesn't suffer from #998 and #999 either.

  10. carlescufi commented on Jun 29, 2018

    @carlescufi

    It's also mentioned as a feature in https://reviewable.io/:
    "Clearly shows net deltas since last time you looked, even if com­mits got re­based or amended"

    This would be ideal, gerrit-style deltas even when you force-push.

  11. donquixote commented on Jun 29, 2018

    @donquixote

    Agreed. BTW it's easy to delete tags.

    Is it also easy to bulk delete tags? E.g. by wildcard?
    I think a graphical UI might be useful, to let you filter the tags, then select a subset of them, then delete.

    There could be different tag prefixes for "commit lost after rebase" vs "commit referred to in a github comment", so you can delete one category without deleting the other.

    Auto-generated tag names could have a pattern like archive/COMMIT_DATE/COMMIT_ID, so they don't have to be random. More parts could be added for commit author, associated branch name (if there was a branch at the time), PR or issue number.

    Btw one problem with auto-adding tags is that it pollutes your local repository when you git fetch --tag. In most cases, orphan commits are not desirable to keep.

  12. aspiers commented on Jun 29, 2018

    @aspiers
    CollaboratorAuthor
  13. haochenx commented on Oct 16, 2018

    @haochenx

    +1

    This is a real problem for our team. We just migrated from Bitbucket, where they keep and display amendment history of commits. If you have commits A->B->C and e.g. amend B to B' (and thus C becomes C' with a different parent) via interactive rebase and force push the branch, the whole action will be recorded as -C -B +B' +C'. The whole action and all related commits (including B and C), as well as all review messages on those commits are retained and easily accessible from the web UI.

    I personally think the Bitbucket way that is the correct way to implement regarding this issue, since a PR is supposed to be amendable and the amendment history, including any comments, are valuable for historical accounts.

  14. KaelWD commented on Nov 21, 2018

    @KaelWD

    I'd consider this resolved, the test commit is still visible, and force pushes are now shown in the PR history
    image

  15. aspiers commented on Nov 22, 2018

    @aspiers
    CollaboratorAuthor

    @KaelWD It is not necessarily resolved. As I already mentioned, my test commit experiment is very far from definitive, plus GitHub's UI explicitly admits it's an issue. The fact that force-pushes are now displayed does not guarantee that they will resolve to anything useful.

    The only way we can know for sure if this is resolved is if GitHub announces they've fixed it. If noone sees a single instance of the problem in the next few months then yeah maybe it's fixed, but even then there is no way to be sure.

  16. aspiers commented on Nov 22, 2018

    @aspiers
    CollaboratorAuthor

    Having said that, the enhancement you spotted does resolve #998, and partially resolves #999 too, so thanks a lot for reporting it!

  17. added
    parity
    on Mar 3, 2019
  18. lvella commented on Apr 5, 2021

    @lvella

    Just faced this issue in a corporate instance of GitHub: It deleted the previous commit after a forced push, and it couldn't even find the original commit after I pushed it to a different branch.

  19. xnlcasad commented on Aug 25, 2021

    @xnlcasad

    @aspiers Do you know if there are any open items/discussions related to #997, #998, #999 in the https://github.com/github/feedback repository ?
    I'd like to know what's the current status on supporting forced-pushed PR branches on Github. Thanks!

  20. aspiers commented on Aug 25, 2021

    @aspiers
    CollaboratorAuthor

    I don't know; there is supposed to be an official migration as per #1985 but GitHub seem to have gone quiet on that front for now.

  21. aspiers commented on Aug 25, 2021

    @aspiers
    CollaboratorAuthor

    However I have just created a new github/feedback label which we'll use to mark issues which are already reported in https://github.com/github/feedback, so in future you should be able to use that to track issues which are in both repos.

  22. erikeckhardt commented on Aug 26, 2021

    @erikeckhardt

    GitLab does this well, offering a great UI for selecting which two inter-revision diffs in a PR you want to see the changes between. Not sure why GitHub doesn't see this as a valuable and important feature.

  23. aspiers commented on Aug 26, 2021

    @aspiers
    CollaboratorAuthor

    @erikeckhardt I believe you are discussing #999 rather than this issue.

  24. eeckhardt-chwy commented on Aug 27, 2021

    @eeckhardt-chwy

    @erikeckhardt I believe you are discussing #999 rather than this issue.

    It seems you are correct as far as the UI portion. My mistake!

    For the functionality portion, this case #997 does seem like a prerequisite to such a UI working. Not garbage-collecting PR commits from history, despite them being technically "unreachable" from any branch, would at least allow the "force pushed" link in the Conversation log to work, which would provide a UI of some kind.

    For reference:

    username force-pushed the branch_name_here branch from bfe2d5e to ad975e2 3 hours ago

    Theoretically, this could be pretty easily achieved by maintaining a hidden branch per PR that has all PR versions in history. Don't ever display these hidden branches to the user or let them be pulled down to git clients, and garbage collection is automatically prevented from disposing of those commits.

  25. aspiers commented on Aug 27, 2021

    @aspiers
    CollaboratorAuthor

    @eeckhardt-chwy commented on August 27, 2021 8:28 PM:

    @erikeckhardt I believe you are discussing #999 rather than this issue.

    It seems you are correct as far as the UI portion. My mistake!

    No worries ;-)

    For the functionality portion, this case #997 does seem like a prerequisite to such a UI working. Not garbage-collecting PR commits from history, despite them being technically "unreachable" from any branch, would at least allow the "force pushed" link in the Conversation log to work, which would provide a UI of some kind.

    Yes, you're correct. That's why I filed the three issues #997, #998, and #999 in that order ;-) The later ones depend on the earlier ones.

    For reference:

    username force-pushed the branch_name_here branch from bfe2d5e to ad975e2 3 hours ago

    Theoretically, this could be pretty easily achieved by maintaining a hidden branch per PR that has all PR versions in history. Don't ever display these hidden branches to the user or let them be pulled down to git clients, and garbage collection is automatically prevented from disposing of those commits.

    In GitHub's backend, it probably wouldn't even need to be a branch; it could just be a ref, e.g. in the format refs/pr/X/Y where X is the PR number and Y is the number of times the PR branch head has been updated. And this way this family of refs would even be helpful if it wasn't hidden, because it wouldn't pollute the branch list, but it would still be available for download to the local repo to aid with local history navigation. It's worth noting that GitHub clients could already choose to create refs like this, and maybe some already do.

  26. erikeckhardt commented on Aug 28, 2021

    @erikeckhardt

    it could just be a ref, e.g. in the format refs/pr/X/Y where X is the PR number and Y is the number of times the PR branch head has been updated.

    At first I was thinking, no need for the Y part, because we won’t need each history entry to be at a tip. We could just add a commit each time the PR branch was force-pushed to, so long as it brought the index to the correct state. But then I realized that without the same history, the SHA would be different, which wouldn’t prevent garbage collection.

    But, having one ref per PR version would be additionally useful, because sometimes I want to view a diff of a history that never existed, which these entries would help build—if a PR submitter rebases an unmerged PR branch onto the target branch, and there were unpushed local changes included, then the diff between versions includes the rebased changes and obscures the submitter’s changes. While this is best solved by git clients or scripting, it can be worked around by pulling up the ref of the prior version, rebasing that onto the same ref the PR was rebased onto, and finally adding the result as another commit in the previous version’s ref (with the extra step of having to look up this special SHA to get the onubscured diff).

    The only wrinkle in this workaround is that there’s no automatic way to resolve merge conflicts on this version-that-never-existed (but logically should).

    I plan to create some aliases and/or scripts to do the pr-ref-creation automatically that you’ve suggested. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    code reviewparitypull-requests

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions