如何从 Git 仓库中删除那些令人烦恼的 Mac OS X .DS_Store
文件?
从代码库中删除现有的.DS_Store
文件:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
添加这一行:
.DS_Store
将.gitignore
文件添加到您的存储库顶层(如果不存在则创建该文件)。您可以使用以下命令轻松完成此操作:
cd top-directory
touch .gitignore
echo .DS_Store >> .gitignore
然后将文件提交到代码库:
git add .gitignore
git commit -m '.DS_Store banished!'
.DS_Store
添加到.gitignore
文件中是否具有递归效果?也就是说,它是否会忽略some/level/of/folders/.DS_Store
这样的文件? - Charlie Schliesser综合benzado和webmat的回答,结合git rm
的更新,不会因为找到非存储库中的文件而失败,并使其在任何用户处都可以粘贴:
# 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
~/.gitignore
作为全局排除文件会很麻烦。有些文件我想从主目录中排除,但又不希望全局忽略它们。因此,我使用同样的指令 core.excludesfile
,而是使用 ~/.gitexcludes
。 - AL the Xvi ~/.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
编辑:
移除图标,因为它们可能需要作为应用程序资源提交。
在某些情况下,您可能还希望全局忽略一些文件。对我来说,.DS_Store就是其中之一。以下是方法:
git config --global core.excludesfile /Users/mat/.gitignore
(或者选择任何文件)
然后像编辑存储库的.gitignore一样编辑该文件。请注意,我认为您必须使用绝对路径。
如果由于有已暂存的更改而无法删除文件,请使用:
git rm --cached -f *.DS_Store
git rm --cached -f .DS_Store
- Pei步骤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
这行,只添加 .DS_Store
就可以了。 - Can最好的方法是永久性地摆脱这个文件:
创建一个全局的.gitignore
文件:
echo .DS_Store >> ~/.gitignore_global
告诉 Git 你想在所有仓库中使用此文件:
git config --global core.excludesfile ~/.gitignore_global
就是这样!.DS_Store
将在所有存储库中被忽略。
最高得票的答案很棒,但是像我这样的菜鸟需要帮助。下面是如何创建.gitignore文件、编辑、保存、删除你可能已经添加到git的文件,并将文件推送到Github。
创建.gitignore文件
要创建.gitignore文件,只需使用touch
命令创建一个指定名称的空白文件。我们想创建名为.gitignore的文件,所以可以使用以下命令:
touch .gitignore
忽略文件
现在,您必须将告诉git忽略DS Store文件的行添加到.gitignore中。您可以使用nano编辑器完成此操作。
nano .gitignore
Nano非常好用,因为它包含有关如何退出的说明。(按Ctrl-O保存,按Ctrl-X退出)
从此Github gist复制并粘贴一些常见忽略文件的想法。回答这个问题最重要的是:
# OS generated files #
######################
.DS_Store
.DS_Store?
# 这里的 # 是注释,可以帮助您在文件逐渐增大时进行组织。
# 这篇Github文章也有一些通用的想法和指南。
# 从已添加到git的文件中删除
# 最后,您需要实际从目录中删除那些DS Store文件。
# 使用这个最有票数的答案中的伟大命令。它将遍历目录中的所有文件夹,并从git中删除这些文件。
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
# 将.gitignore推送到Github上
# 最后一步,您需要实际提交您的.gitignore文件。
git status
git add .gitignore
git commit -m '.DS_Store banished!'
打开终端并输入 "cd < ProjectPath >"。
删除现有文件:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
nano .gitignore
添加此内容 .DS_Store
输入 "ctrl + x"
输入 "y"
按 Enter 保存文件
git add .gitignore
git commit -m '.DS_Store removed.'
nano .gitignore
如果文件不存在则创建该文件,该命令应从项目根目录执行。 - Saif我必须将上述命令中的"git-rm"更改为"git rm"才能使其正常工作:
find . -depth -name '.DS_Store' -exec git rm --cached '{}' \; -print