从 Git 的 master 分支删除提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31193531/
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
Delete commits from the master branch in Git
提问by polydegmon
I forgot to make a branch before making some code updates and now I have to revert to the initial commit and remove all the commits after the initial one. The work has been saved off somewhere so I'm not at risk of losing it.
在进行一些代码更新之前我忘记创建一个分支,现在我必须恢复到初始提交并在初始提交之后删除所有提交。这项工作已保存在某处,因此我没有丢失它的风险。
I've used the "git reset --hard {SHA}" to reset back to where I need to be, however now I have all the commits that were performed after the initial commit waiting to come back down and sync with my master copy.
我已经使用“git reset --hard {SHA}”重置回我需要的位置,但是现在我拥有在初始提交后执行的所有提交等待返回并与我的主副本同步.
I want to delete all commits from 6/10/2015 to 7/2/2015 as they are no longer needed. Does anyone know how to do this?
我想删除从 6/10/2015 到 7/2/2015 的所有提交,因为不再需要它们。有谁知道如何做到这一点?
回答by polydegmon
Removing Remote Commits From Any Branch [Master is a branch]
从任何分支中删除远程提交 [Master 是一个分支]
If you commit something to the remote server that for whatever reason should not have been committed on the particular branch you can remove it using the following steps
如果您向远程服务器提交了一些不应该在特定分支上提交的内容,您可以使用以下步骤将其删除
If the branch has changes you want to keep - Start at step 1
如果分支有您想要保留的更改 - 从第 1 步开始
If you don't care about the changes and simply want to revert to a specific commit - Start at step 3
如果您不关心更改而只想恢复到特定的提交 - 从第 3 步开始
Perform a hard reset and reset the HEAD to the commit you want to create the branch from using the command : git reset --hard {SHA}
- {SHA} is the commit ID
Create the branch and publish it to the server (You risk losing your work if you skip this step)
Perform a hard reset to the commit that you want to keep as the HEAD - git reset --hard {SHA}
- This will result in all the commits that were done after the commit you reset to, will be pending as an incoming sync - Do not sync
To nuke the incoming commits perform a force push using the command : git push -f
执行硬重置并将 HEAD 重置为您要使用以下命令创建分支的提交:git reset --hard {SHA}
- {SHA} 是提交 ID
创建分支并将其发布到服务器(如果跳过此步骤,您可能会丢失工作)
对要保留为 HEAD 的提交执行硬重置 - git reset --hard {SHA}
- 这将导致在您重置为提交后完成的所有提交,将作为传入同步挂起 - 不同步
要核对传入的提交,请使用以下命令执行强制推送: git push -f
回答by David Deutsch
If nobody else uses this repo, do a git push -f
and the server's contents will be overwritten with your contents.
如果没有其他人使用此 repo,请执行 agit push -f
并且服务器的内容将被您的内容覆盖。
If other people areusing this repository (and have pulled since you made the commits you want to remove), they will need to do a hard reset on their end as well after pulling (git reset --hard origin/master
, assuming you are on branch master
). I would suggest in this case to use git revert
to simply undo the effect of your commits instead of trying to eliminate them from the history.
如果其他人正在使用此存储库(并且在您进行了要删除的提交后已拉取),则他们也需要在拉取后对其进行硬重置(git reset --hard origin/master
假设您在分支上master
)。在这种情况下,我建议使用git revert
简单地撤消提交的影响,而不是试图将它们从历史记录中消除。