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


当前回答

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

并在其中添加以下行

.DS_Store

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

其他回答

有时.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

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

.DS_Store
gensrc
!custom/**

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

结合benzado和webmat的答案,使用git rm进行更新,不会对发现的不在repo中的文件失败,并使其可为任何用户粘贴:

# remove any existing files from the repo, skipping over ones not in repo
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
# specify a global exclusion list
git config --global core.excludesfile ~/.gitignore
# adding .DS_Store to that list
echo .DS_Store >> ~/.gitignore

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

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

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

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

删除忽略的文件:

(.DS_Store)

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