Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove last commit from remote git repository [duplicate]

Tags:

git

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]

Thanks.

like image 489
user1059540 Avatar asked Nov 22 '11 10:11

user1059540


People also ask

How do I remove last commit from remote repository?

To undo the last commit from a remote git repository, you can use the git reset command. command. This will undo the last commit locally. command to force push the local commit which was reverted to the remote git repository.

How do I remove a last commit in git?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

How do I get rid of a remote commit?

Delete a remote commit. To remove a commit you already pushed to your origin or to another remote repository you have to first delete it locally like in the previous step and then push your changes to the remote. Notice the + sign before the name of the branch you are pushing, this tells git to force the push.


2 Answers

Be careful 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, most likely 'master'>
like image 111
knittl Avatar answered Oct 13 '22 10:10

knittl


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.

like image 21
Michael Krelin - hacker Avatar answered Oct 13 '22 12:10

Michael Krelin - hacker