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


当前回答

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

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

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

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

其他回答

有时.DS_Store文件位于远程存储库中,但在本地项目文件夹中不可见。要解决此问题,我们需要删除所有缓存的文件并再次添加。

步骤1:将其添加到.gitignore文件中。

# Ignore Mac DS_Store files
.DS_Store
**/.DS_Store

步骤2:删除缓存的文件,然后使用这些命令再次添加。

git rm -r --cached .
git add .
git commit -am "Removed git ignored files"
git push -f origin master

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

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

(或您选择的任何文件)

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

从存储库中删除现有的.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!'

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

并在其中添加以下行

.DS_Store

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

我必须将上面的git rm更改为git rm才能使其工作:

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