.gitignore 仅在本地

.gitignore only on local

我想知道如何git忽略文件,但仅限于本地文件,而不推送它(因为从事该项目的其他人不应该得到 git忽略文件的更新我在那里添加了。

简单来说,在 git 状态之后我只有 :

modified:   .gitignore

因为我添加了要忽略的文件到我的 gitignore 但现在我希望上面的行不用推就消失了。

.gitignore 文件包含要阻止 committed/pushed 到远程仓库的文件和文件夹列表。

但是,提交并推送 .gitignore 文件总是可以接受的。这是一种非常普遍的做法。因此,提交 .gitignore 文件并继续您的代码。

阅读此 thread 以获得 reference/good 提交 .gitignore 文件的实践。

应该提交 Gitignore 文件,因为在大多数情况下,您忽略的内容也会被团队中的其他开发人员忽略。

但如果您绝对需要将其排除在跟踪本地更改之外,您可以执行以下操作:

git update-index --assume-unchanged .gitignore

这将使它从修改后的列表中 "disappear"。尽管如果其他人更改它,您将不会在拉动时得到更改。然后您需要执行以下操作以将其带回跟踪列表并再次拉取:

git update-index --no-assume-unchanged .gitignore

对于本地忽略 you should use the .git/info/exclude file,而不是 .gitignore:

Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file.

这两个文件接受相同的格式。

VSCode 中的另一个选项是使用 Git Exclude 扩展,它基本上可以通过右键单击将任何 file/folder 添加到您的 .git/info/exclude 文件