0

I want to delete the last 10 commits(and pushed too) I have done in my repo

Past I have tried this

git push origin HEAD --force 

But I think this will delete all the commits . How to just delete the last n commits from the git completely after I have pushed it .. ?

xenex-media
  • 173
  • 1
  • 15

1 Answers1

1

I would recommend that you don't try to delete them but instead git revert them. You will then create a new commit which removes the content of those few commits, and the operation will remain visible in the history (helping everyone understand what's going on).

$ git revert --no-commit HEAD^
$ git revert --no-commit HEAD^^
$ git revert --no-commit HEAD^^^
...
$ git commit -m "revert last ten commits"
Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 1
    Yes, this is good practice. Maybe consider the short syntax variant `git revert -n HEAD~10..` to revert the ten last. Or preferably log your last commits, store the hash of the last "good commit", and do `git revert -n ..` (mind the two dots at the end, this is the syntax to imply a range between something and `HEAD`). Don't forget to `add` any conflict resolutions, and commit at the end. – Romain Valeri Dec 19 '19 at 11:08