如何从Git存储库中删除.DS_Store文件?

时间:2020-03-06 14:28:51  来源:igfitidea点击:

如何从Git存储库中删除那些烦人的Mac OS X.DS_Store文件?

解决方案

这将起作用:

find . -name "*.DS_Store" -type f -exec git-rm {} \;

它将删除所有名称以" .DS_Store"结尾的文件,包括" ._。DS_Store"。

使用git-rm删除它们,然后将.DS_Store添加到.gitignore中以阻止再次添加它们。我们还可以使用blueharvest阻止它们一起创建

从存储库中删除现有文件:

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

添加行

.DS_Store

到.gitignore文件中,该文件可以在存储库的顶层找到(如果尚未存在,则创建该文件)。我们可以在顶层目录中使用此命令轻松完成此操作

echo .DS_Store >> .gitignore

然后

git add .gitignore
git commit -m '.DS_Store banished!'

在某些情况下,我们可能还希望全局忽略某些文件。对我来说,.DS_Store是其中之一。这是如何做:

git config --global core.excludesfile /Users/mat/.gitignore

(或者我们选择的任何文件)

然后像回购的.gitignore一样编辑文件。请注意,我认为我们必须使用绝对路径。

我发现snipplr的以下行最适合擦除所有.DS_Store,包括具有本地修改的行。

find . -depth -name '.DS_Store' -exec git-rm --cached '{}' \; -print

--cached选项,保留本地.DS_Store,因为无论如何都会将其复制。

就像上面提到的,将.DS_Store添加到项目根目录下的.gitignore文件中。然后,它将不再在视线范围内。

我不得不在上面将git-rm更改为git rm,以使其正常工作:

find . -depth -name '.DS_Store' -exec git rm --cached '{}' \; -print