git push --force 和如何处理它
Ever been in a situation where the wrong Git command has had a chaotic impact on your project’s repo? People make mistakes! …but sometimes, these mistakes can cost hours of your team’s time. So, in this post, we’ll show you how to quickly recover from an unfortunate git push --force
.
曾经遇到过错误的Git命令对项目仓库产生混乱影响的情况吗?人们会犯错!...但有时候,这些错误可能会浪费团队数小时的时间。因此,在本文中,我们将向您展示如何快速从不幸的git push --force
中恢复。
Editor’s note: This post was originally published in 2017, but it’s been overhauled for 2024!
编辑注:本文最初发表于2017年,但已在2024年进行了全面改版!
Now, if you’re thinking, “this doesn’t sound like something I’d ever do”, well, let’s not get overly confident. Sooner or later, this is going to happen. When working with several remotes in the same Git repository, eventually, you will git push --force
into main
(or another important branch that should’ve never been messed with).
现在,如果您认为“这听起来不像是我会做的事情”,好吧,我们不要过于自信。迟早会发生这种情况。当在同一个Git存储库中使用多个远程时,最终您将git push --force
到main
(或其他不应该被搞乱的重要分支)。
This might happen, for instance, when deploying with Heroku, which uses separate Git remotes to build and deploy an application. Following a long day of work, it could be incredibly easy to execute git push --force
instead of the usual git push --force heroku main
.
这可能会发生在使用Heroku部署时,Heroku使用单独的Git远程来构建和部署应用程序。在工作了一整天后,执行git push --force
而不是通常的git push --force heroku main
可能非常容易。
…and oops! In the blink of an eye, your teammates have lost all their latest work. Time to face their rage.
…哎呀!眨眼之间,您的队友们失去了他们的最新工作。现在要面对他们的愤怒了。
However, as an excellent guide from a separate field famously reminds us, “DON’T PANIC”! After all, the upside is that you’re using Git, and that means everything can be fixed.
然而,正如一位来自另一个领域的优秀指南所提醒我们的那样,“DON’T PANIC”!毕竟,好处是你正在使用Git,这意味着一切都可以修复。
Let’s start with the best case scenario: someone else working on the same code pulled a recent version of the main
branch just before you broke it. In this case, all you have to do is to go into your team chat and ask this person to force push their recent changes.
让我们从最理想的情况开始:其他人在您破坏之前刚好拉取了main
分支的最新版本。在这种情况下,您只需进入团...