回滚远程 Git 存储库

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

Rolling back a remote Git repository

git

提问by Jake

I have a remote Git repository, and I need to roll back the last ncommits into cold oblivion.

我有一个远程 Git 存储库,我需要将最后一次n提交回滚到冷遗忘状态。

回答by elmarco

You can use git revert <commit>…for all the n commits, and then push as usual, keeping history unchanged.

您可以git revert <commit>…对所有 n 次提交使用,然后像往常一样推送,保持历史不变。

Or you can "roll back" with git reset --hard HEAD~n. If you are pushing in a public or shared repository, you may diverge and break others work based on your original branch. Git will prevent you doing so, but you can use git push -fto force the update.

或者您可以使用git reset --hard HEAD~n. 如果您正在推送公共或共享存储库,您可能会根据您的原始分支分散并破坏其他工作。Git 会阻止您这样做,但您可以使用它git push -f来强制更新。

回答by Pat Notz

elmarco is correct... his suggestion is the best for shared/public repositories (or, at least public branches). If it wasn't shared (or you're willing to disrupt others) you can also push a particular ref:

elmarco 是正确的……他的建议最适合共享/公共存储库(或至少是公共分支)。如果它没有被共享(或者你愿意扰乱他人),你也可以推送一个特定的引用:

git push origin old_master:master

Or, if there's a particular commit SHA1 (say 1e4f99e in abbreviated form) you'd like to move back to:

或者,如果有一个特定的提交 SHA1(比如缩写形式的 1e4f99e),你想回到:

git push origin 1e4f99e:master

回答by geedoubleya

Fortunately I was in a position to use Pat Notz's solutionwhich completely removed the unwanted commit. However, initially I got the error

幸运的是,我能够使用Pat Notz 的解决方案,它完全删除了不需要的提交。但是,最初我得到了错误

error: failed to push some refs to 'ssh://[email protected]'
To prevent you from losing history, non-fast-forward updates were rejected*

But adding the force (-f) option overwrite this error

但是添加 force ( -f) 选项会覆盖此错误

git push -f origin 52e36b294e:master

回答by Hazok

If you have direct access to the remote repo, you could always use:

如果您可以直接访问远程存储库,则可以始终使用:

git reset --soft <sha1>

This works since there is no attempt to modify the non-existent working directory. For more details please see the original answer:

这是有效的,因为没有尝试修改不存在的工作目录。更多详情请看原答案:

How can I uncommit the last commit in a git bare repository?

如何取消提交 git 裸存储库中的最后一次提交?