忽略已在远程存储库中的 files/folders 的问题 (github)

problem with ignoring files/folders that are already in the remote repository (github)

我正在研究 PhpStorm。我希望 git 忽略一些 files/folders。这些也在远程存储库中。我只是不想反映关于它们的任何更多更改,但它们必须从远程回购中保留 - 而不是被删除,因为我要从这个回购中拉到生产服务器。

我做错了什么?

注意:.idea 文件夹对于生产服务器可能不重要,但代码需要一些重要文件夹 运行。

不要使用 --assume-unchanged 忽略跟踪的文件

--assume-unchanged 标志的目的是告诉 Git 避免检查特定路径中的文件是否有更改,因为它们 不应该更改 .

来自documentation

setting this bit on a path does not mean Git will check the contents of the file to see if it has changed — it makes Git to omit any checking and assume it has not changed.

此功能作为性能优化存在,以防您的存储库有非常大的文件and/or 慢速文件系统。

作为 Junio C Hamano points out in a discussion thread about the difference between .gitignore and --assume-unchanged:

Assume-unchanged should not be abused for an ignore mechanism. It is "I know my filesystem operations are slow. I'll promise Git that I won't change these paths by making them with that bit.

他接着说:

Especially, it is not a promise by Git that Git will always consider these paths are unmodified---if Git can determine a path that is marked as assume-unchanged has changed without incurring extra lstat(2) cost, it reserves the right to report that the path has been modified (as a result, git commit -a is free to commit that change).

换句话说,如果修改了标记为 --assume-unchanged 的路径中的文件,Git 可能决定忽略该标志,如果这样做不是太慢的话。这可能就是您看到 .idea/ 目录中的文件已修改的原因。

改用--skip-worktree

还有另一种方法可以告诉 Git 忽略路径中的更改,那就是设置 --skip-worktree 标志。

来自documentation:

When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

您可以将 entry 替换为 path--skip-worktree--assume-unchanged 之间的区别在于它们所做的 假设 :虽然 --assume-unchanged 期望文件不会被修改,但 --skip-worktree根本不关心工作目录中文件的状态。

documentation 说的也是:

The working directory version may be present or absent. If present, its content may match against the index version or not.

一旦路径被标记为 --skip-worktree,Git 将不遗余力地从回购历史中排除对工作目录中文件所做的任何本地修改。

我发现我哪里错了。对于那些可能需要的人,我会用我有的两个文件夹来解释:

  • config/ 和 config.php 在它下面
  • pdf/ 和 pdf.css 在它下面

所以:

git update-index --skip-worktree config/

运行 这个和改变 config.php 没有帮助。另一方面:

git update-index --skip-worktree pdf/

在 运行 之后,我从我的 Web 应用程序获得了一个 pdf 输出,它也在 pdf/ 文件夹下创建了 pdf.html。所以,这个新文件被忽略了,很好。

很快,我得出结论,如果这是我想要忽略的文件,我必须指出确切的文件,如下所示:

git update-index --skip-worktree config/config.php

它忽略此文件之后的任何更改。

(我对 --assume-unchanged 进行了同样的尝试,它起作用了,但由于我已经从上面的答案中很好地理解了差异,所以我想在这里强调更好的方法)