491

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]
5
  • If you have committed it to the repo then I don't think there is a way to remove it. Nov 22, 2011 at 10:15
  • @Guarav: I think there is no polite way to remove it. I mean, if all else fails, rm -rf on the repo can trash anything. But I think the question I linked to contains a much nicer solution -- that is still impolite if anyone else has pulled the rev.
    – sarnold
    Nov 22, 2011 at 10:17
  • 2
    The last commit can always be removed from your history. Depending on many factor you may or may not wipe it from people memory completely. Nov 22, 2011 at 10:18
  • @sarnold, if it was pushed elsewhere you only trash your idea of repository, not the world :) Nov 22, 2011 at 10:19
  • @Michael: ha! excellent distinction.
    – sarnold
    Nov 22, 2011 at 10:20

2 Answers 2

1197

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~
8
  • 39
    Just a note, in zsh use git reset HEAD\^ Apr 10, 2014 at 4:38
  • 58
    Use git reset HEAD~1 on Win machines
    – æ-ra-code
    Apr 10, 2014 at 13:43
  • 3
    what does +HEAD do? When I did git log it added (HEAD -> approval, origin/approval) on the latest commit. what does that mean?
    – ASN
    Oct 3, 2017 at 2:22
  • 3
    @ASN: it force-pushes the currently checked out branch (which was/is approval in your case).
    – knittl
    Oct 3, 2017 at 5:46
  • How to handle 'alternate reality' on other's PC
    – Usman
    Aug 12, 2020 at 11:06
29

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.

6
  • 5
    You could also do it if somebody pulled it already. Nov 19, 2014 at 10:34
  • 8
    Absolutely. Though you should only do it if you know what you're doing and what the consequences are. And if do you probably aren't reading this answer. Nov 19, 2014 at 13:01
  • which one is the branch name here? before colon or after colon Oct 30 at 17:23
  • @VikasAcharya, both are, first local branch, then remote branch. Oct 30 at 21:14
  • 1
    @VikasAcharya, it's completely up to you, you can create local branch any way you want and push to any remote branch. Nov 1 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.