git 如何删除 GitHub 上的提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/448919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:04:41  来源:igfitidea点击:

How can I remove a commit on GitHub?

gitgithubgit-commit

提问by hectorsq

I "accidentally" pushed a commit to GitHub.

我“不小心”向 GitHub 推送了一个提交。

Is it possible to remove this commit?

是否可以删除此提交?

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

我想恢复我的 GitHub 存储库,就像这次提交之前一样。

采纳答案by Can Berk Güder

Note:please see alternative to git rebase -iin the comments below—

git reset --soft HEAD^

注意:请参阅下面git rebase -i评论中的替代方案——

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~2and delete the second line within the editor window that pops up.

首先,删除本地存储库上的提交。您可以使用git rebase -i. 例如,如果这是您的最后一次提交,您可以git rebase -i HEAD~2在弹出的编辑器窗口中执行并删除第二行。

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

然后,使用强制推送到 GitHub git push origin +branchName --force

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

有关更多信息,请参阅Git 魔法第 5 章:历史教训 - 然后是一些(即,如果您想删除较旧的提交)。

Oh, and if your working tree is dirty, you have to do a git stashfirst, and then a git stash applyafter.

哦,如果你的工作树很脏,你必须先做git stash,然后再做git stash apply

回答by Dustin

git push -f origin HEAD^:master

That should "undo" the push.

那应该“撤消”推送。

回答by CodeWalrus

For an easy revert if it's just a mistake (perhaps you forked a repo, then ended up pushing to the original instead of to a new one) here's another possibility:

如果只是一个错误(也许你分叉了一个 repo,然后最终推送到原始而不是新的),为了轻松恢复,这是另一种可能性:

git reset --hard 71c27777543ccfcb0376dcdd8f6777df055ef479

Obviously swap in that number for the number of the commit you want to return to.

显然,将该数字交换为您要返回的提交的编号。

Everything since then will be deleted once you push again. To do that, the next step would be:

一旦您再次推送,此后的所有内容都将被删除。为此,下一步将是:

git push --force

回答by kate

  1. git logto find out the commit you want to revert

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

  1. git log找出您要还原的提交

  2. git push origin +7f6d03:master而 7f6d03 是错误推送提交之前的提交。 +是为了force push

And that's it.

就是这样。

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

是一个很好的指南,可以解决您的问题,简单易行!

回答by Loukan ElKadi

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:

1 - 从日志中复制您想要返回的提交引用:

git log

2 - Reset git to the commit reference:

2 - 将 git 重置为提交参考:

 git reset <commit_ref>

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

3 - 隐藏/存储来自错误提交的本地更改,以便在推送到远程后稍后使用:

 git stash

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

4 - 将更改推送到远程存储库,(-f 或 --force):

git push -f

5 - Get back the stored changes to local repository:

5 - 将存储的更改取回本地存储库:

git stash apply

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

7 - 如果更改中有未跟踪/新文件,则需要在提交之前将它们添加到 git:

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:

6 - 添加您需要的任何额外更改,然后提交所需的文件,(或使用点“.”而不是说明每个文件名,以提交本地存储库中的所有文件:

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

or

或者

git commit -m "<new_commit_message>" .

回答by subutux

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/

您需要清除缓存才能将其完全擦除。来自 git 的这个帮助页面会帮助你。(它帮助了我) http://help.github.com/remove-sensitive-data/

回答by Jyoti Prakash

Use git revertfor reverting your push.

使用git revert用于恢复您的推动。

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

git-revert - 还原一些现有的提交

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).

恢复相关补丁引入的更改,并记录一些记录它们的新提交。这要求你的工作树是干净的(没有来自 HEAD 提交的修改)。

回答by Hilen

1. git reset HEAD^ --hard
2. git push origin -f

This work for me.

这对我有用。

回答by Ekambaram E

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

回答by george mano

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

关联