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


当前回答

步骤1

这将删除目录(包括子目录)中的每个.DS_Store文件

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

步骤2

将此添加到.gitignore以防止根目录和每个子目录中的任何DS_Store文件进入git!

**/.DS_Store

从git文档中:

前导“**”后接斜杠表示所有目录中的匹配。例如,“**/foo”与任何位置的文件或目录“foo”匹配,与模式“foo“相同。“**/foo/bar”匹配直接位于目录“foo”下的文件或目录“bar”。

其他回答

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

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

添加此行:

.DS_Store

到文件.gitignore,该文件可以在存储库的顶层找到(如果文件还不存在,则创建该文件)。您可以在顶部目录中使用以下命令轻松完成此操作:

echo .DS_Store >> .gitignore

然后将文件提交到repo:

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

以下方法对我来说效果最好。处理了不匹配的文件,以及本地修改的文件。作为参考,这是在运行git 1.7.4.4的Mac 10.7系统上。

查找并删除:

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

我还通过设置全局core.excludsfile来全局忽略.DS_Store。

首先,创建文件(如果还不存在):

touch ~/.gitignore

然后添加以下行并保存:

.DS_Store

现在配置git以全局尊重文件:

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

如果您使用的是GitHub Desktop应用程序,则此方法有效。

如果还没有,请在应用程序中打开存储库(¨O)从“存储库”选项卡中,选择“存储库设置”。转到“忽略的文件”并添加您希望忽略的文件。

使用touch.gitignore命令创建.gitignore文件

并在其中添加以下行

.DS_Store

保存.gitignore文件,然后将其推送到git repo中。

$ git commit -m "filter-branch --index-filter 'git rm --cached --ignore-unmatch .DS_Store"
$ git push origin master --force