Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a commit on GitHub? [duplicate]

I "accidentally" pushed a commit to GitHub.

Is it possible to remove this commit?

I want to revert my GitHub repository as it was before this commit.

like image 564
hectorsq Avatar asked Jan 15 '09 23:01

hectorsq


People also ask

Can you delete a commit on GitHub?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

How do you get rid of an old commit?

First, remove the commit on your local repository. You can do this using git rebase -i . For example, if it's your last commit, you can do git rebase -i HEAD~2 and delete the second line within the editor window that pops up. Then, force push to GitHub by using git push origin +master .

How do you remove some commits from a branch?

You can simply remove that commit using option "d" or Removing a line that has your commit. In the latest git version there is no more option d. You need just remove lines with commits from rebase to delete them.


2 Answers

Note: please see an alternative to git rebase -i in the comments below—

git reset --soft HEAD^

First, remove the commit on your local repository. You can do this using git rebase -i. For example, if it's your last commit, you can do git rebase -i HEAD~2 and delete the second line within the editor window that pops up.

Then, force push to GitHub by using git push origin +branchName --force

See Git Magic Chapter 5: Lessons of History - And Then Some for more information (i.e. if you want to remove older commits).

Oh, and if your working tree is dirty, you have to do a git stash first, and then a git stash apply after.

like image 87
Can Berk Güder Avatar answered Sep 25 '22 07:09

Can Berk Güder


git push -f origin HEAD^:master 

That should "undo" the push.

like image 24
Dustin Avatar answered Sep 26 '22 07:09

Dustin