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.

  • Push local repo to new sub-directory of remote repo
  • devtools::install_git Over SSH
  • How can I write a rule in gitignore to ignore all files except directories in some directory?
  • How to handle upload folder in git & wordpress devlopment workflow
  • gitignore - only allow certain extensions and files
  • How should i sort out versioning and offline testing on a website
  • 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?

  • Trouble using capistrano 3 while deploying
  • github no address associated with name
  • Is there a way to recover a commit that was accidentally skipped during a rebase?
  • Can I hg clone a git repository from BitBucket?
  • How to undelete a branch on github?
  • Failed to copy Manifest
  • 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.