.gitignore忽略每个文件夹和子文件夹中的所有.DS_Store文件

879
我已将.DS_Store添加到.gitignore文件中,但似乎它只忽略根目录下的.DS_Store文件,而不是每个文件夹和子文件夹中的文件。
如何解决这个问题?

4
可能是如何从Git存储库中删除.DS_Store文件?的重复问题。 - user2062950
1
你是如何将它添加到.gitignore的?它应该在所有目录中都起作用(对我来说是这样的)。 - Thilo
1
它对我也起作用。我也试过将其放在文件末尾,如果你必须有一个(特定于平台的)换行符,但这并没有改变任何层次结构中任何部分内的 .DS_Store 目录仍然被忽略的情况。 - enorl76
1
据我理解,问题是:如何轻松忽略所有子目录中 .DS_Store 的出现,而不必在每个子目录中手动执行此操作。我也遇到了同样的问题。下面的答案展示了如何解决它(最后两段)。 - petrsyn
12个回答

1020

我认为你遇到的问题是,在之前的某个提交中,你不小心将.DS_Store文件添加到了代码库中。当然,一旦一个文件被跟踪到你的代码库中,即使它与适用的.gitignore文件中的条目匹配,它也会继续被跟踪。

你需要手动删除已添加到代码库中的.DS_Store文件。你可以使用

git rm --cached .DS_Store

移除后,git 应该会忽略它。你只需要在根目录下的 .gitignore 文件中添加以下代码:.DS_Store。不要忘记句号!

git rm --cached .DS_Store

仅从当前目录中删除.DS_Store文件。您可以使用

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

从仓库中删除所有的.DS_Stores

提示:由于您可能永远不想包括.DS_Store文件,因此请制定全局规则。首先,在某个地方创建全局.gitignore文件,例如

echo .DS_Store >> ~/.gitignore_global

现在告诉Git对所有仓库使用它:

git config --global core.excludesfile ~/.gitignore_global

这个页面帮助我回答了你的问题。


75
最后两段回答了这个问题。谢谢。 - petrsyn
81
´´git rm --cached .DS_Store´´ 只会从当前目录中移除一个 .DS_Store 文件。使用 ´´find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch´´ 命令可以从仓库中移除所有的 .DS_Store 文件。 - auco
5
提供一个其他常见需忽略的文件列表可能会很方便。 - geotheory
22
在我看来,使用全局规则是个坏主意:试图在多个用户/机器之间维护全局配置从来都不奏效——你需要将管理存储库的规则放在存储库本身中。我同意这种做法 - Yarin
24
我不同意你的看法,尽管我点赞了你的评论,因为我认为它很有见地。如果我们进行全局操作,那么每个Mac用户只需要这样做一次,但如果我们在存储库范围内操作,那么我们需要针对每个存储库进行操作。我不知道你怎么想,但是我经常创建新的存储库。通过全局操作,可以一劳永逸地解决问题。 - Edward Newell
显示剩余6条评论

466

**/.DS_Store添加到.gitignore文件中,以忽略子目录中的.DS_Store文件。


如果.DS_Store已经提交:

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
忽略它们在所有存储库中的存在:(有时命名为._.DS_Store
echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

1
仍然上传到我的代码库中吗? - Freddy Sidauruk
2
@FreddySidauruk 首先使用 git rm --cached your_file 命令。 - ronald8192
1
@FreddySidauruk 应该是 find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch,来自: https://dev59.com/WGMl5IYBdhLWcg3wcmvD#_qugEYcBWogLw_1bRJFU - ronald8192
2
如果您在相关文件夹中有打开的文件,则似乎需要添加“-f”:find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch - Mehmet Kaplan
1
并且同样可以在一行中完成:printf "%b\n" ".DS_Store\n._.DS_Store\n**/.DS_Store\n**/._.DS_Store\n" >> ~/.gitignore_global && git config --global core.excludesfile ~/.gitignore_global - ikaerom
1
在一行中也是一样的:printf "%b\n" ".DS_Store\n._.DS_Store\n**/.DS_Store\n**/._.DS_Store\n" >> ~/.gitignore_global && git config --global core.excludesfile ~/.gitignore_global - undefined

262
如果您的git存储库从未添加.DS_Store,请将其添加到.gitignore文件中。如果没有该文件,请创建一个名为的文件。
.gitignore

在你的应用程序的根目录中,只需编写:

**/.DS_Store

在这里。这将永远不允许 .DS_Store 文件偷偷潜入你的 Git。

但是,如果它已经存在,可以在终端中输入以下命令:

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

接着提交并推送更改,以从您的远程存储库中删除 .DS_Store 文件:

git commit -m "Remove .DS_Store from everywhere"

git push origin master

现在需要将 .DS_Store 添加到你的 .gitignore 文件中,然后再使用最后两个代码片段进行提交和推送(git commit..., git push...)。


3
为什么使用双星号?那代表什么意思? - MilanG
2
@MilanG是一个关键字,基本上意味着“在此目录及其所有子目录中搜索”。 - lachie_h
1
不确定这是对双星号的完美解释。它是通配符。根据您想要实现的目标,可以使用 ***。这是告诉 shell 如何导航目录的一种方式。此 stackoverflow 答案完整地解释了它 https://dev59.com/jV4c5IYBdhLWcg3wOoAH#28199633 - El'Magnifico
1
将永远无法匹配隐藏(./)目录:https://facelessuser.github.io/wcmatch/glob/ - Jonathan
1
这很酷,我也使用 find . -name '.DS_Store' -type f -delete 递归删除所有创建的 .DS_Store 文件。 - Robby Alvian Jaya Mulia
显示剩余2条评论

110

你的.gitignore文件应该像这样:

# Ignore Mac DS_Store files
.DS_Store
只要您不包括斜杠,它就会与所有目录中的文件名匹配。 (来自这里)

1
它仍然上传到存储库中。 - Freddy Sidauruk
3
@FreddySidauruk 然后你需要运行 git rm --cached path/to/file/here - Sgnl

95

只需将此内容放在新的一行中的 .gitignore 文件中即可。

**/.DS_Store

来自git文档

  • 以斜杠跟着一个星号 "**" 表示匹配所有目录。例如,"**/foo" 匹配文件或目录 "foo" 在任何地方,和模式 "foo" 相同。"**/foo/bar" 匹配直接在目录 "foo" 下的任何位置上 "bar" 文件或目录。

8
正如您引用的文档所述,**/.DS_Store 只是写成 .DS_Store 的一种更复杂的方式。只有在想要 gitignore 特定路径模式时才需要双星号,无论它出现在哪里。例如,**/foo/.DS_Store 只会隐藏 foo 子目录中的 .DS_Store 文件(不管 foo 位于何处)。 - orlp
3
@orlp一直都是这样吗?我曾经发誓过添加了.DS_Store到.gitignore一百万次,但是.DS_Store仍然出现在子目录中。在测试了你的信息之后,看来你是绝对正确的。这改变了我对这个SO问题的想法。但更普遍地说:有一个(高)风险会意外忽略子目录中与其他地方有意忽略的文件同名的文件。似乎git这样做是不好的(尽管方便)。感谢你指出这一点。 - stevec

33

第一步,删除所有的*.DS_store文件。可以运行以下命令:

git rm -f *.DS_Store

但要注意,如果你输错了,rm -f 命令可能会有点危险!

*.DS_Store
.DS_Store

加入到 .gitignore 文件中。这对我有用!


18

*.DS_Store添加到您的.gitignore文件中。对于我来说,这样做非常完美。


14

你也可以在auco的答案中添加--cached 标志,以保留本地 .DS_store 文件,就像 Edward Newell 在他的原始回答中提到的那样。 修改后的命令如下:find . -name .DS_Store -print0 | xargs -0 git rm --cached --ignore-unmatch ..祝福和感谢!


9

步骤1:使用以下命令删除现有文件

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

步骤2:在.gitignore文件中添加.DS_Store

步骤3:提交您的更改到.gitignore git add .gitignore git commit -m "removed .DS_Store"


7
  1. $ git rm ./*.DS_Store - 从git中删除所有的.DS_Store文件
  2. $ echo \.DS_Store >> .gitignore - 忽略以后的.DS_Store文件

提交并推送


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接