有没有办法防止由于编辑器没有自动更新而在 git 拉取后意外覆盖?

Is there a way to prevent accidental overwrite after git pull due to editor not updating automatically?

当文件在编辑器(比如 Sublime 或 Atom)中打开,并且文件在编辑器外编辑时,编辑器总是拒绝刷新它显示的文件。这种情况很少发生,因为人们很可能只会在特定时间范围内使用单一工具来编辑文件。当文件为只读时,这显然不是问题。例如读取系统错误日志时,文件会更新为系统运行,可能会有新的错误日志,但不会编辑日志文件,所以不会造成冲突。

但是,git pull更新文件时会出现问题。

当某人将最新更新拉到存储库时,他可能会在编辑器中打开一个文件,该文件已在更新中进行了一些更改。如果编辑器刷新文件失败,文件将保存旧内容,任何新的更改都将丢失。

有时,使用 sourcetree 反转 hunk 很烦人,但是当有许多文件更新时,覆盖可能会被推送到 git 服务器而不被注意 — 直到发生错误。目前我们正在使用git log --follow -p -- file命令来定位和恢复错误,但如果不及时发现覆盖是不可能的,迫使我们手动复制行。有没有办法首先防止这种覆盖?

When a folder is opened by an editor (mainly Sublime or Atom in our team), and the code is edited outside the editor, sometimes the content in the editor is refreshed, but sometimes it doesn't.

这正是 Atom issue 3594

中讨论的内容

file-watcher 这样的 Atom 包将有助于缓解这个问题,如果检测到编辑器外的修改,它会提示重新加载每个文件。

您对 SublimeText 有同样的问题 reported in this thread。 如 that thread 中所述,当通过网络共享访问文件时,该问题在 Windows 上更为相关。
File Reloader 可以提供帮助,但无法检测到外部变化。

SublimeText thread提到设置(2016)

{ "always_prompt_for_file_reload": true }

但是当编辑器和保存的文件都发生变化时,这可能无济于事: 解决了这个问题:

If there are changes on both sides (from disk and through the editor) when ever you try to save the file using VSCode, the editor will warn you about that situation and a file comparison will allow you to decide what to do.

这就是为什么使用 SublimeText(除了 "always_prompt_for_file_reload" 设置),您可能需要 FileDiff plugin.
它确实允许使用 Saved:

比较文件

Is there a way to prevent this kind of overwrite at the first place?

是的,因为您的编辑器无法通过文件系统的中断进行检测,然后通过每 3 秒定期轮询文件系统来执行它们。

在 Notepad++ 上编程时,这种故障非常明显,这就是为什么真正需要一个定期检查文件系统以了解文件更改的轮询插件的原因。

对于 Sublime TextNotepad++,插件是:

  1. https://packagecontrol.io/packages/Auto%20Refresh
  2. https://superuser.com/a/592913/458103 (文件监控=>开始监控)

我用了很长时间,因为程序在多个文本 editors/IDE 上使用相同的文件。然后,当在编辑器之间交替时,我大多数时候会因为他们不从文件系统重新加载文件而丢失我的工作。但是,在安装上述插件后,我再也没有遇到过这个问题,也没有注意到由于定期重新加载而导致的性能问题。