-1

I have a repository with about 16 commits, all pushed to the remote server, all only committed on the master branch.

I want the last 3 commits completely erased as if they never happened, so that someone looking at the repo will only see the first 13. I don't even want to know about the last three.

How do I do this? I see so many different answers for different situations. I just need help for this specific scenario.

Ryan
  • 393
  • 7
  • 22
  • You can force push: https://stackoverflow.com/questions/10510462/force-git-push-to-overwrite-remote-files – noymer Jan 08 '18 at 00:02
  • 1
    You can find your answer here: https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git – Donald Shahini Jan 08 '18 at 00:02
  • 3
    Possible duplicate of [Delete commits from a branch in Git](https://stackoverflow.com/questions/1338728/delete-commits-from-a-branch-in-git) – Rumid Jan 08 '18 at 00:05
  • You should probably revert those 3 commits. If you nuke them and force push, then anyone else using the branch will certainly know something is awry. – Tim Biegeleisen Jan 08 '18 at 00:06
  • Will a `git reset --hard` leave a history of those commits? @DonaldShahini @Rumid – Ryan Jan 08 '18 at 00:09
  • @TimBiegeleisen won't that still show the history of the commits? I'm the only one working on this repo – Ryan Jan 08 '18 at 00:10
  • @TimBiegeleisen That is definitely not what the OP is asking about. History rewriting is dangerous but it is sometimes necessary. That said, you *should* be extra-sure that really is what you want. Why do you want to completely erase those commits? – Daniel H Jan 08 '18 at 00:28
  • @DanielH This is why I am suggesting _not_ to rewrite history, and to instead use `git revert`. Anyway, in either case if the branch be already shared others would know that something has happened. – Tim Biegeleisen Jan 08 '18 at 01:53

1 Answers1

1

With

git reset --hard HEAD~3

you will delete the 3 last commits, but since you have already pushed it to remote, you will need to force push it.

git push origin HEAD --force

Be aware that if others have already pulled these commits, when they pull again the deleted commits will get merged and might be pushed back.

As a side note, git revert is usually the best way to go since it will not rewrite history, avoiding possible complications.

Nogoseke
  • 969
  • 1
  • 12
  • 24