If a commit is in a common / stable branch (that is, the one that is used by at least someone else, usually master , develop and others), then it cannot be deleted. You can only create a “canceling” commit with git revert ( for more, see here, point 5 ).
Perhaps your commit contained some critical data, such as passwords or keys. In this case, it is useless to try to save them by removing the commit that contains them. The data has already been compromised, all passwords and keys will have to be changed.
# отменяем последний коммит, доступный по указателю HEAD git revert HEAD # фиксируем отмену в новом коммите git commit -m'reverted the last commit'
If you are absolutely sure that you have launched an “extra” commit into your own branch, that no one has locked it into a stable branch and just didn’t start development from your last commit, you can roll back the local branch to the previous commit, and then rewrite the changes in the deleted one ( read more here, clause 4.2 )
Attention! Never do this with shared branches ( master , develop and others).
If it is absolutely necessary, be sure and immediately warn everyone who works with this repository. After rewriting the last commit in a stable branch, they will have to manually update this branch on their machine. If they have any new commits whose ancestor is the commit to be deleted, they will have to rebase to the commit.
git reset HEAD^ git push -f
Here you have my favorite scheme illustrating different solutions.
