How to completely remove a merged pull request?

I merged a pull request on GitHub. However, it was a mistake, and I want to undo the merge, its commits, and most importantly, delete all the files introduced by these several commits from the history.

I reverted the commit with another one and it works, but the files are still in the commit history (they’re big) and it would be nice if the commits themselves disappeared.

  • Which files generated from Ionic do I include in source control?
  • Checking out a remote git branch that does not exist locally?
  • Git - Is it possible to restore to the state before I resolve conflicts wrong
  • Add Github fork to existing repository
  • Git revert of merge commit causes issues when merge is actually done.
  • git bundle: two-way all branches sync between local and remote repo
  • I’ve tried to filter-branch removing the files, but it doesn’t seem to be shrinking the repository size (and the commits are still there).

    What should I do?

  • Git + Rails: How to restore files deleted with “git rm -r”?
  • git get only list of the last i.e. 2 commit
  • Can't make git stop tracking package-lock.json
  • How to develop multiple pip packages in a git project?
  • Auto pushing to a specific folder on GitHub or other Git service
  • Committing specific parts of a file in Git
  • 4 Solutions collect form web for “How to completely remove a merged pull request?”

    Remove the merge and the commits with

    git reset --hard <sha1>

    as described by @knittl. However, you won’t see any change in your repository size until you delete the associated dangling objects. You also need to expire the reflog from keeping a reference to those objects with e.g.

    git reflog expire --expire-unreachable=now --all
    git prune
    git repack -a -d

    Reverting the commit will not remove the files from history.

    If you want the files not only to not exist in the current HEAD, but also to never have existed at all, git revert isn’t the right command for you. You need to run a filter-branch to remove those files completely.

    You said you’ve already done this, but you don’t show us how; if you correctly do the following:

    • git filter-branch --tree-filter to remove the files
    • git reflog expire --expire={1 second}
    • rm -rf .git/refs/original
    • git gc

    then repository should shrink. You need the last three steps because (in order):

    • The original commits are reachable via your reflog
    • References to the original commits are stored by filter-branch in refs/original
    • Until you run a garbage collection, the “loose” objects aren’t deleted

    If you just want to remove the merge and it’s associated commits, use git reset --hard:

    git reset --hard <commit before merge>

    Note that this will destroy any uncommitted changes in your current HEAD. It will also remove all commits that happened after the merge (so maybe you are better off using git rebase).

    After resetting your branch, force-push it to make the new history persistent.

    DISCLAIMER: It’s bad practice to rewrite already published history. Don’t do this, if you don’t have to.

    Using tree-filter or index-filter didn’t work. Hard resetting and repacking didn’t either.

    Here’s what worked:

    git checkout <commit>

    git push --force origin HEAD:master

    Now I’m curious as to why those solutions didn’t work. I was checking using the size-pack in git count-objects -v. Is this correct? Should I see a reduction in size right away or only after pushing to origin?

    Git Baby is a git and github fan, let's start git clone.