为什么对远程存储库进行强制推送会有危险吗? [英] Why is it dangerous to do a force-push against a remote repository?

查看:50
本文介绍了为什么对远程存储库进行强制推送会有危险吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我进行了一些更改,将其提交,然后将提交推送到远程存储库.然后,我决定忘记对它进行适当的注释,或者拼写错误,然后要进行软重置以编辑并重新提交更改.

Let's say I've made some changes, committed them, and then pushed my commit to the remote repository. Then I decide that I forgot to comment it adequately, or misspelled something, and I want to do a soft reset to edit and re-commit my changes.

是什么导致将其强制推送到远程存储库的危险"?

What makes it "dangerous" to force-push this to the remote repository?

推荐答案

Git会尽力 不会覆盖您的任何更改,因此在日常例行程序中,如果您尝试执行以下操作:推送到其HEAD参考与本地HEAD参考不同的远程存储库中,Git会对此进行调整.

Git tries really hard to not overwrite any of your changes, so in your day-to-day routine, if you attempt to push to a remote repository whose HEAD reference differs from your local HEAD reference, Git will pitch a fit about it.

使用--force时,即使您不完全了解或欣赏动作的后果,Git也会松开训练轮,并假定您知道自己在做什么.

When you use --force, Git takes the training wheels off and assumes that you know what you're doing, even if you don't fully understand or appreciate the ramifications of your actions.

为说明您的示例,这是远程存储库上的内容:

To illustrate your example, this is what your remote repository has on it:

<-- [] <-- [] <-- [HEAD]

...这是您推送后但未发现错误之前的本地地址:

...and this is your local after you've pushed, but before you discovered your mistake:

<-- [] <-- [] <-- [HEAD]

当您使用--amend纠正错误时,Git实际上有两个引用前一个提交的提交,但是只有其中一个引用了规范的HEAD引用.

When you went to correct the mistake by using --amend, Git actually has two commits that refer to the previous one, but only one of them refers to the canonical HEAD reference.

<-- [] <-- [] <-- [HEAD]
            \
             \<-- [(old HEAD)] 

新HEAD引用的SHA与远程存储库中的SHA不匹配,因此Git会拒绝推送,因为它认为您将丢失数据.

The SHA of your new HEAD reference doesn't match what's on your remote repository, so Git will refuse to push since it thinks you're going to lose data.

但是,当您强制使用它时,Git会信任您,并使用新的HEAD作为规范参考. 因此,丢失了对旧HEAD提交的引用.

When you force it, though, Git trusts you and takes the new HEAD as the canonical reference. Through this, the reference to the old HEAD commit is lost.

<-- [] <-- [] <-- [HEAD]

这很危险,因为:

  • 可能丢失了数据;如果它不是简单的--amend,则可能不会,但是仍然有可能

  • You could have lost data; the likelihood is that you didn't if it's a simple --amend, but it's still possible

任何其他基于旧HEAD引用进行工作的人现在都必须重新整理其工作,这可能会造成很多不必要的胃灼热.

Anyone else that's based their work off of the old HEAD reference now has to rebase their work, which can make for a lot of undue heartburn.

通信可以减轻第二部分的负担,但是在强制推动时第一部分需要多加注意.只有在绝对确定要推送的内容不会造成数据或信息丢失的情况下,才可以强行推入.

Communication can mitigate the second piece, but the first piece requires a bit more caution when force-pushing. Only force-push if you're absolutely certain that what you're pushing won't cause a loss in data or information.

这篇关于为什么对远程存储库进行强制推送会有危险吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆