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

Save your local changes first somewhere on the side ( backup )

You can browse your recent commits, then select a commit hash by clicking on "Copy the full SHA" button to send it to the clipboard.

If your last commit hash is, let's say g0834hg304gh3084gh ( for example )

You have to run:

git push origin +g0834hg304gh3084gh:master

Using the hash that you've copied earlier to make it the "HEAD" revision.

Add your desired local changes. Done ;)

查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 05:18

Use git revert for reverting your push.

git-revert - Revert some existing commits

git revert [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>...
git revert --continue
git revert --quit
git revert --abort

Revert the changes that the related patches introduce, and record some new commits that record them. This requires your working tree to be clean (no modifications from the HEAD commit).

查看更多
余生无你
4楼-- · 2018-12-31 05:21
1. git reset HEAD^ --hard
2. git push origin -f

This work for me.

查看更多
谁念西风独自凉
5楼-- · 2018-12-31 05:23
  1. git log to find out the commit you want to revert

  2. git push origin +7f6d03:master while 7f6d03 is the commit before the wrongly pushed commit. + was for force push

And that's it.

Here is a very good guide that solves your problem, easy and simple!

查看更多
千与千寻千般痛.
6楼-- · 2018-12-31 05:23

Find the ref spec of the commit you want to be the head of your branch on Github and use the following command:

git push origin +[ref]:[branchName]

In your case, if you just want to go back one commit, find the beginning of the ref for that commit, say for example it is 7f6d03, and the name of the branch you want to change, say for example it is master, and do the following:

git push origin +7f6d03:master

The plus character is interpreted as --force, which will be necessary since you are rewriting history.

Note that any time you --force a commit you could potentially rewrite other peoples' history who merge your branch. However, if you catch the problem quickly (before anyone else merges your branch), you won't have any issues.

查看更多
泪湿衣
7楼-- · 2018-12-31 05:24

In case you like to keep the commit changes after deletion:

Note that this solution works if the commit to be removed is the last committed one.


1 - Copy the commit reference you like to go back to from the log:

git log

2 - Reset git to the commit reference:

 git reset <commit_ref>

3 - Stash/store the local changes from the wrong commit to use later after pushing to remote:

 git stash

4 - Push the changes to remote repository, (-f or --force):

git push -f

5 - Get back the stored changes to local repository:

git stash apply

7 - In case you have untracked/new files in the changes, you need to add them to git before committing:

git add .

6 - Add whatever extra changes you need, then commit the needed files, (or use a dot '.' instead of stating each file name, to commit all files in the local repository:

git commit -m "<new_commit_message>" <file1> <file2> ...

or

git commit -m "<new_commit_message>" .
查看更多
登录 后发表回答