How can I remove a commit on GitHub?

2018-12-31 04:47发布

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.

21条回答
时光乱了年华
2楼-- · 2018-12-31 05:03

In GitHub Desktop you can just right click the commit and revert it, which will create a new commit that undoes the changes.

The accidental commit will still be in your history (which may be an issue if, for instance, you've accidentally commited an API key or password) but the code will be reverted.

This is the simplest and easiest option, the accepted answer is more comprehensive.

查看更多
荒废的爱情
3楼-- · 2018-12-31 05:04

Run this command on your terminal.

git reset HEAD~n

You can remove the last n commits from local repo e.g. HEAD~2. Proceed with force git push on your repository.

git push -f origin <branch>

Hope this helps!

查看更多
心情的温度
4楼-- · 2018-12-31 05:06
git push -f origin HEAD^:master

That should "undo" the push.

查看更多
无色无味的生活
5楼-- · 2018-12-31 05:07

You need to know your commit hash from the commit you want to revert to. You can get it from a GitHub URL like: https://github.com/your-organization/your-project/commits/master

Let's say the hash from the commit (where you want to go back to) is "99fb454" (long version "99fb45413eb9ca4b3063e07b40402b136a8cf264"), then all you have to do is:

git reset --hard 99fb45413eb9ca4b3063e07b40402b136a8cf264
git push --force
查看更多
柔情千种
6楼-- · 2018-12-31 05:08

Delete the most recent commit, keeping the work you've done:

git reset --soft HEAD~1

Delete the most recent commit, destroying the work you've done:

git reset --hard HEAD~1
查看更多
若你有天会懂
7楼-- · 2018-12-31 05:11

You'll need to clear out your cache to have it completely wiped. this help page from git will help you out. (it helped me) http://help.github.com/remove-sensitive-data/

查看更多
登录 后发表回答