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


当前回答

解决此问题的最佳解决方案是全局忽略系统上所有git repo中的这些文件。这可以通过创建一个全局gitignore文件来完成,如:

vi ~/.gitignore_global

添加忽略文件的规则,如:

# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

现在,将此文件添加到全局git配置中:

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

编辑:

删除了图标,因为它们可能需要作为应用程序资产提交。

其他回答

我发现snipplr的下面一行在擦除所有.DS_Store方面做得最好,包括一个具有本地修改的行。

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

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

正如上面提到的,将.DS_Store添加到项目根目录的.gitignore文件中。那么它将不再在你的视线中(回购)。

对于那些没有得到上述任何方法帮助的人,请尝试更彻底地检查您的.gitignore,它可能会在目录和子目录之间有一些规则组合,因此烦人的.DS_Store文件不会仅在这些文件夹中被忽略。例如,您希望忽略除自定义目录中的文件夹外的gensrc文件夹,因此可以使用以下.gitignore:

.DS_Store
gensrc
!custom/**

因此,在这个设置中,忽略了任何/path/.DS_Store,而不是自定义/gensrc/.DS_Store,修复程序将把.DS_Store条目移动到.gitignore文件的底部。

如果您无法删除这些文件,因为它们有暂存的更改,请使用:

git rm --cached -f *.DS_Store

如果要将DS_Store文件删除到每个文件夹和子文件夹:如果已提交DS_Store:

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

通过以下方式忽略它们:

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

在某些情况下,您可能还需要全局忽略某些文件。对我来说,.DS_Store就是其中之一。以下是操作方法:

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

(或您选择的任何文件)

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