Delete a commit from github

29,350

Use git rebase process before applying new changes. After rebasing add the new changes and commit.

git rebase -i HEAD~2

git push origin +master
Share:
29,350
Jistanidiot
Author by

Jistanidiot

Updated on May 31, 2020

Comments

  • Jistanidiot
    Jistanidiot almost 4 years

    Possible Duplicate:
    How can I remove a commit on github?
    How to delete a 'git commit'

    So I'm trying to remove a commit from github.

    It is 3 commits ago and I'm ok if all the changes are lost.

    So based on questions from here, I did a

    git reset --hard <sha-commit-name-to-go-back-to>
    git push -f origin HEAD^:master
    

    Now it is gone from github as well as in git log.

    I then redid my changes and did

    git add <file I changed>
    git commit
    git log
    

    Everything looks fine. There's no trace of the old commits. So I finally did a:

    git push
    

    Now suddenly github has those three commits that I had removed.

    What am I doing wrong? How to I keep the commits from returning?

  • Jistanidiot
    Jistanidiot almost 12 years
    Thanks! This seems to have worked. However digging around in the .git directory I found a bunch of files under log with references to the commit I want to remove. I also edited those files to remove the reference before doing the git push origin +master.
  • kostix
    kostix almost 12 years
    @Jistanidiot, most probably you were messing with git reflog's storage. Not too bright idea in fact as git reflog has its own ways to manipulate its storage, and you had changes to break something.
  • Kenny Cason
    Kenny Cason about 11 years
    Thanks! I first did a git reset --hard <commit_number> then used your commands and it worked like a charm!
  • user1063287
    user1063287 almost 8 years
    I did git reset --hard <sha-commit-name-to-go-back-to> , git push -f origin HEAD^:master and then git rebase -i HEAD~2 and nano editor opens file: File: /var/www/html/site-name/.git/rebase-merge/git-rebase-todo with options to pick, reword, edit etc. I don't know what to do...