Suppose you have 3 commits as follows:
commit 3
commit 2
commit 1
where the last commit commit 3
was wrong, then you can execute:
git reset --hard HEAD~1
You will find, HEAD is now at commit 2
.
Then use git push --force
to force this change to the server. This way the last bad commit on the server is completely gone.
It is worth noting that this type of operation is more dangerous. For example, after your commit 3, someone else submits a new one
commit 4
. After you force the push, the friend's onecommit 4
also disappears.
Quoted from https://segmentfault.com/q/1010000002898735