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:11

It is not very good to re-write the history. If we use git revert <commit_id>, it creates a clean reverse-commit of the said commit id.

This way, the history is not re-written, instead, everyone knows that there has been a revert.

查看更多
旧时光的记忆
3楼-- · 2018-12-31 05:12

Add/remove files to get things the way you want:

git rm classdir
git add sourcedir

Then amend the commit:

git commit --amend

The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place

Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.

查看更多
忆尘夕之涩
4楼-- · 2018-12-31 05:13

For GitHub - Reset your commits (HARD) in your local repo and create a new branch. Push new . Delete OLD branch. (Make new one as the default branch if you are deleting the master branch)

查看更多
临风纵饮
5楼-- · 2018-12-31 05:14

To preserve the branching and merging structure is important to use the --preserve-merges option when doing the rebase:

git rebase --preserve-merges -i HEAD^^
查看更多
伤终究还是伤i
6楼-- · 2018-12-31 05:15

Note: please see 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.

查看更多
冷夜・残月
7楼-- · 2018-12-31 05:16

To delete the commit from the remote repository:

 git push -f origin last_known_good_commit:branch_name

In order delete the commit from your local repository:

git reset --hard HEAD~1

link

查看更多
登录 后发表回答