user1059540
user1059540

Reputation: 5523

Remove last commit from remote Git repository

How can I remove the last commit from a remote Git repository such as I don't see it any more in the log?

If for example git log gives me the following commit history

A->B->C->D[HEAD, ORIGIN]

how can I go to

A->B->C[HEAD,ORIGIN]

Upvotes: 552

Views: 523723

Answers (2)

knittl
knittl

Reputation: 265221

Be aware that this will create an "alternate reality" for people who have already fetch/pulled/cloned from the remote repository. But in fact, it's quite simple:

git reset HEAD^ # remove commit locally
git push origin +HEAD # force-push the new HEAD commit

If you want to still have it in your local repository and only remove it from the remote, then you can use:

git push origin +HEAD^:"$name_of_your_branch" # e.g. +HEAD^:master

Some shells interpret the ^ character. For those shells, either quote/escape or use ~:

HEAD\^
'HEAD^'
HEAD~

Upvotes: 1308

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143081

If nobody has pulled it, you can probably do something like

git push remote +branch^1:remotebranch

which will forcibly update the remote branch to the last but one commit of your branch.

Upvotes: 29

Related Questions