-1

I have checked in code into my local master git repo and then pushed it up to origin. There is only 1 such erroneous checkin. What is the best way to revert the last commit locally and remotely?

I'd prefer to destroy the history.

Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

2 Answers2

3

If you are sure nobody has done a fetch/pull from the repo since you made the commit, do:

git reset --hard HEAD~1
git push -f

If someone has already fetched/pulled, however, there is not much you can do without their cooperation. In that case, your best bet is doing a git revert HEAD, which will make an additional commit that undoes your last commit.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
2

You can rewrite history to remove that commit by using git rebase -i <any earlier commit>.
If it's the most recent commit, you can also just run git reset --hard HEAD^.

You can then run git push -f to rewrite the remote history. This will break anyone who pulled that commit.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964