Search code examples
gitgithubrevision-history

remove commits (forever) from git history


I would like to permanently remove specified commits from the entire history of a git repository. I saw this thread, in which the following is recommended:

git reset --hard HEAD~4
git push origin HEAD --force

That's fine and resets the status of my repository, but in no way does it remove my commits. I would like to literally roll back history and discard changes to the repo since 4 revisions ago.

Could anyone help me with this? When I try the above and look at the revision history in Github, I still see unwanted commits sitting there.

I saw this article, but I wanted to check whether or not there were other options before investigating this solution.


Solution

  • From your question, what actually happens when you run

    git push origin HEAD --force
    

    is unclear, but I can think of at least two reasons why it may fail to force-push master to origin:

    1. the currently checked out branch is not master and is a branch already up-to-date on origin, or
    2. you have a detached HEAD.

    Make sure master is indeed the currently checked-ou branch, by running git checkout master; then run your force push command. Alternatively, specify master explicitly in the git push command:

    git push --force origin master
    

    That should do it.