10

I have pushed some changes to the repository.

I want to delete the last commit from the repository and permanently remove the history entry as well.

How can I do this?

Opal
  • 81,889
  • 28
  • 189
  • 210
n00b.exe
  • 287
  • 1
  • 3
  • 12
  • 1
    Possible duplicate of [Rolling back local and remote git repository by 1 commit](https://stackoverflow.com/questions/4647301/rolling-back-local-and-remote-git-repository-by-1-commit) – phd Nov 15 '17 at 00:00

1 Answers1

22

Try:

git checkout <branch> // replace branch with a name of the branch you worked on
git reset --hard HEAD~1 // this command removes the latest commit
git push --force-with-lease origin <branch> // push the changes to the remote

If nobody modified the remote while doing the operation above your push will be accepted, otherwise it may be rejected. Further steps are dependant on if you can use -f instead of --force-with-lease. If so do it, but it will overwrite other's changes. If not - it can't be done without changing the history.

Opal
  • 81,889
  • 28
  • 189
  • 210