从 Git 的分支中删除提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1338728/
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:57:04  来源:igfitidea点击:

Delete commits from a branch in Git

gitgit-rebasegit-reset

提问by hap497

I would like to know how to delete a commit.

我想知道如何删除提交。

By delete, I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch.

通过delete,我的意思是就好像我没有做出那个提交,并且当我将来进行推送时,我的更改不会推送到远程分支。

I read git help, and I think the command I should use is git reset --hard HEAD. Is this correct?

我阅读了 git help,我认为我应该使用的命令是git reset --hard HEAD. 这样对吗?

回答by gahooa

Careful:git reset --hardWILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keepbefore running this command.

小心:git reset --hard将删除您的工作目录更改。在运行此命令之前,请务必存储您想要保留的任何本地更改

Assuming you are sitting on that commit, then this command will wack it...

假设你正坐在那个提交上,那么这个命令会搞砸它......

git reset --hard HEAD~1

The HEAD~1means the commit before head.

HEAD~1意味着在 head 之前提交。

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

或者,您可以查看 的输出git log,找到要备份的提交的提交 ID,然后执行以下操作:

git reset --hard <sha1-commit-id>


If you already pushed it, you will need to do a force push to get rid of it...

如果您已经推动它,则需要进行强制推动才能摆脱它......

git push origin HEAD --force

However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

但是,如果其他人可能已经拉了它,那么你最好开始一个新的分支。因为当他们拉动时,它只会将其合并到他们的工作中,而您将再次将其推回原位。

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.

如果您已经推送,最好使用git revert, 创建将撤消更改的“镜像”提交。但是,两个提交都将在日志中。



FYI -- git reset --hard HEADis great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.

仅供参考 -git reset --hard HEAD如果您想摆脱正在进行的工作,那就太好了。它会将您重置回最近的提交,并清除工作树和索引中的所有更改。



Lastly, if you need to find a commit that you "deleted", it is typically present in git reflogunless you have garbage collected your repository.

最后,如果您需要找到您“删除”的提交,git reflog除非您对存储库进行了垃圾回收,否则它通常会出现在其中。

回答by Greg Hewgill

If you have not yet pushed the commit anywhere, you can use git rebase -ito remove that commit. First, find out how far back that commit is (approximately). Then do:

如果您尚未将提交推送到任何地方,则可以使用git rebase -i删除该提交。首先,找出提交的时间(大约)。然后做:

git rebase -i HEAD~N

The ~Nmeans rebase the last Ncommits (Nmust be a number, for example HEAD~10). Then, you can edit the file that Git presents to you to delete the offending commit. On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.

~N装置变基最后N提交(N必须是一个数,例如HEAD~10)。然后,您可以编辑 Git 提供给您的文件以删除违规提交。在保存该文件时,Git 将重写所有以下提交,就好像您删除的提交不存在一样。

The Git Book has a good section on rebasingwith pictures and examples.

Git Book 有一个很好的部分关于用图片和示例进行变基。

Be careful with this though, because if you change something that you havepushed elsewhere, another approach will be needed unless you are planning to do a force push.

不过要小心,因为如果你改变了你别处推送的东西,除非你计划进行强制推送,否则将需要另一种方法。

回答by 1800 INFORMATION

Another possibility is one of my personal favorite commands:

另一种可能性是我个人最喜欢的命令之一:

git rebase -i <commit>~1

This will start the rebase in interactive mode -iat the point just before the commit you want to whack. The editor will start up listing all of the commits since then. Delete the line containing the commit you want to obliterate and save the file. Rebase will do the rest of the work, deleting only that commit, and replaying all of the others back into the log.

这将在-i您想要重击的提交之前以交互模式启动 rebase 。编辑器将开始列出此后的所有提交。删除包含要删除的提交的行并保存文件。Rebase 将完成其余的工作,仅删除该提交,并将所有其他提交重放回日志中。

回答by Rob

I'm appending this answer because I don't see why anyone who has just tried to commit work would want to delete all that work because of some mistake using Git!

我附上这个答案是因为我不明白为什么刚刚尝试提交工作的人会因为使用 Git 的一些错误而想要删除所有工作!

If you want to keep your work and just 'undo' that commit command (you caught before pushing to repo):

如果您想保留您的工作并只是“撤消”该提交命令(您在推送到回购之前捕获):

git reset --soft HEAD~1

Do not use the --hardflag unless you want to destroy your work in progress since the last commit.

不要使用 --hard标志,除非您想破坏自上次提交以来正在进行的工作。

回答by raittes

Removing an entire commit

删除整个提交

git rebase -p --onto SHA^ SHA

Obviously replace "SHA" with the reference you want to get rid of. The "^" in that command is literal.

显然,将“SHA”替换为您想要删除的引用。该命令中的“^”是字面意思。

http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep

http://sethrobertson.github.io/GitFixUm/fixup.html#change_deep

回答by Jakub Nar?bski

If you didn't publish changes, to remove latest commit, you can do

如果您没有发布更改,要删除最新提交,您可以执行

$ git reset --hard HEAD^

(note that this would also remove all uncommitted changes; use with care).

(请注意,这也会删除所有未提交的更改;请谨慎使用)。

If you already published to-be-deleted commit, use git revert

如果您已经发布了待删除的提交,请使用git revert

$ git revert HEAD

回答by tk_

Say we want to remove commits 2 & 4 from the repo.

假设我们要从 repo 中删除提交 2 和 4。

commit 0 : b3d92c5
commit 1 : 2c6a45b
commit 2 : <any_hash>
commit 3 : 77b9b82
commit 4 : <any_hash>

Note: You need to have admin rights over the reposince you are using --hardand -f.

注意:您需要对 repo 拥有管理员权限,因为您使用的是--hard-f

  • git checkout b3d92c5Checkout the last usable commit.
  • git checkout -b repairCreate a new branch to work on.
  • git cherry-pick 77b9b82Run through commit 3.
  • git cherry-pick 2c6a45bRun through commit 1.
  • git checkout masterCheckout master.
  • git reset --hard b3d92c5Reset master to last usable commit.
  • git merge repairMerge our new branch onto master.
  • git push -f origin masterPush master to the remote repo.
  • git checkout b3d92c5签出最后一个可用的提交。
  • git checkout -b repair创建一个新的分支来处理。
  • git cherry-pick 77b9b82运行提交 3。
  • git cherry-pick 2c6a45b运行提交 1。
  • git checkout master结账师傅。
  • git reset --hard b3d92c5将 master 重置为上次可用的提交。
  • git merge repair将我们的新分支合并到 master 上。
  • git push -f origin master将 master 推送到远程仓库。

回答by sun34

git reset --hard commitId

git push <origin> <branch> --force

PS: CommitId refers the one which you want to revert back to

PS:CommitId 指的是您要恢复到的那个

回答by IliasT

Forcefully Change History

强行改变历史

Assuming you don't just want to delete the last commit, but you want to delete specific commits of the last n commits, go with:

假设您不只是想删除最后一次提交,而是想删除最后 n 次提交的特定提交,请执行以下操作:

git rebase -i HEAD~<number of commits to go back>, so git rebase -i HEAD~5if you want to see the last five commits.

git rebase -i HEAD~<number of commits to go back>,所以git rebase -i HEAD~5如果你想查看最后五次提交。

Then in the text editor change the word pickto dropnext to every commit you would like to remove. Save and quit the editor. Voila!

然后在文本编辑器中字改变pickdrop旁边的每一个承诺,你想删除。保存并退出编辑器。瞧!

Additively Change History

附加更改历史记录

Try git revert <commit hash>. Revertwill create a newcommit that undoes the specified commit.

试试git revert <commit hash>Revert将创建一个新的提交来撤销指定的提交。

回答by Anurag-Sharma

If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:

如果要修复最新的提交,可以通过执行以下操作来撤消提交并取消暂存其中的文件:

git reset HEAD~1

This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.

这将使您的存储库返回到暂存文件的 git add 命令之前的状态。您的更改将在您的工作目录中。HEAD~1 指的是当前分支尖端下方的提交。

If you want to uncommit N commits, but keep the code changes in your working directory:

如果您想取消提交 N 次提交,但将代码更改保留在您的工作目录中:

git reset HEAD~N

If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.

如果您想摆脱最新的提交,并且不想保留代码更改,您可以进行“硬”重置。

git reset --hard HEAD~1

Likewise, if you want to discard the last N commits, and do not want to keep the code changes:

同样,如果您想丢弃最后 N 次提交,并且不想保留代码更改:

git reset --hard HEAD~N