如何从 Git 存储库中删除 .DS_Store 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/107701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I Remove .DS_Store files from a Git repository?
提问by John Topley
How can I remove those annoying Mac OS X .DS_Store
files from a Git repository?
如何.DS_Store
从 Git 存储库中删除那些烦人的 Mac OS X文件?
回答by benzado
Remove existing files from the repository:
从存储库中删除现有文件:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Add the line
添加行
.DS_Store
to the file .gitignore
, which can be found at the top level of your repository (or created if it isn't there already). You can do this easily with this command in the top directory
到文件.gitignore
,它可以在您的存储库的顶层找到(或者如果它不存在则创建)。您可以在顶级目录中使用此命令轻松完成此操作
echo .DS_Store >> .gitignore
Then
然后
git add .gitignore
git commit -m '.DS_Store banished!'
回答by Turadg
Combining benzado and webmat's answers, updating with git rm
, not failing on files found that aren't in repo, and making it paste-able generically for any user:
结合 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
回答by Nerve
The best solution to tackle this issue is to Globally ignore these files from all the git repos on your system. This can be done by creating a global gitignore file like:
解决此问题的最佳解决方案是全局忽略系统上所有 git 存储库中的这些文件。这可以通过创建一个全局 gitignore 文件来完成,例如:
vi ~/.gitignore_global
Adding Rules for ignoring files like:
添加忽略文件的规则,例如:
# 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
Now, add this file to your global git config:
现在,将此文件添加到您的全局 git 配置中:
git config --global core.excludesfile ~/.gitignore_global
Edit:
编辑:
Removed Icons as they might need to be committed as application assets.
删除了图标,因为它们可能需要作为应用程序资产提交。
回答by webmat
In some situations you may also want to ignore some files globally. For me, .DS_Store is one of them. Here's how:
在某些情况下,您可能还想全局忽略某些文件。对我来说,.DS_Store 就是其中之一。就是这样:
git config --global core.excludesfile /Users/mat/.gitignore
(Or any file of your choice)
(或您选择的任何文件)
Then edit the file just like a repo's .gitignore. Note that I think you have to use an absolute path.
然后像 repo 的 .gitignore 一样编辑文件。请注意,我认为您必须使用绝对路径。
回答by Reggie Pinkham
If you are unable to remove the files because they have changes staged use:
如果您无法删除文件,因为它们有更改分阶段使用:
git rm --cached -f *.DS_Store
回答by Karthick Vadivel
Open terminal and type "cd < ProjectPath >"
打开终端并输入“cd < ProjectPath >”
Remove existing files:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
nano .gitignore
Add this
.DS_Store
type "ctrl + x"
Type "y"
Enter to save file
git add .gitignore
git commit -m '.DS_Store removed.'
删除现有文件:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
nano .gitignore
添加这个
.DS_Store
输入“ctrl + x”
输入“y”
输入保存文件
git add .gitignore
git commit -m '.DS_Store removed.'
回答by Joshua Dance
Top voted answer is awesome, but helping out the rookies like me, here is how to create the .gitignore file, edit it, save it, remove the files you might have already added to git, then push up the file to Github.
高票答案很棒,但可以帮助像我这样的新手,这里是如何创建 .gitignore 文件,编辑它,保存它,删除您可能已经添加到 git 的文件,然后将文件推送到 Github。
Create the .gitignore file
创建 .gitignore 文件
To create a .gitignore file, you can just touch
the file which creates a blank file with the specified name. We want to create the file named .gitignore so we can use the command:
要创建 .gitignore 文件,您可以只touch
创建具有指定名称的空白文件的文件。我们要创建名为 .gitignore 的文件,以便我们可以使用以下命令:
touch .gitignore
touch .gitignore
Ignore the files
忽略文件
Now you have to add the line which tells git to ignore the DS Store files to your .gitignore. You can use the nano editor to do this.
现在您必须将告诉 git 忽略 DS Store 文件的行添加到您的 .gitignore。您可以使用 nano 编辑器来执行此操作。
nano .gitignore
nano .gitignore
Nano is nice because it includes instructions on how to get out of it. (Ctrl-Oto save, Ctrl-Xto exit)
Nano 很好,因为它包含有关如何摆脱它的说明。( Ctrl-O保存,Ctrl-X退出)
Copy and paste some of the ideas from this Github gistwhich lists some common files to ignore. The most important ones, to answer this question, would be:
复制并粘贴此Github gist中的一些想法,其中列出了一些要忽略的常见文件。要回答这个问题,最重要的是:
# OS generated files #
######################
.DS_Store
.DS_Store?
The # are comments, and will help you organize your file as it grows.
# 是注释,可帮助您在文件增长时组织文件。
This Github articlealso has some general ideas and guidelines.
这篇Github 文章也有一些一般性的想法和指南。
Remove the files already added to git
删除已经添加到 git 的文件
Finally, you need to actually remove those DS Store files from your directory.
最后,您需要从目录中实际删除这些 DS Store 文件。
Use this great command from the top voted answer. This will go through all the folders in your directory, and remove those files from git.
使用来自最高投票答案的这个伟大的命令。这将遍历您目录中的所有文件夹,并从 git 中删除这些文件。
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Push .gitignore up to Github
将 .gitignore 推送到 Github
Last step, you need to actually commit your .gitignore file.
最后一步,您需要实际提交 .gitignore 文件。
git status
git status
git add .gitignore
git add .gitignore
git commit -m '.DS_Store banished!'
git commit -m '.DS_Store banished!'
回答by David Kahn
I had to change git-rm to git rm in the above to get it to work:
我必须在上面将 git-rm 更改为 git rm 才能使其工作:
find . -depth -name '.DS_Store' -exec git rm --cached '{}' \; -print
回答by Nathan
delete them using git-rm
, and then add .DS_Store to .gitignore
to stop them getting added again. You can also use blueharvestto stop them getting created all together
使用 删除它们git-rm
,然后添加 .DS_Store.gitignore
以阻止它们再次被添加。您还可以使用blueharvest来阻止它们一起创建
回答by Nathan
In case you want to remove DS_Store files to every folder and subfolder:
如果您想将 DS_Store 文件删除到每个文件夹和子文件夹:
如果已提交 DS_Store:
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
Ignore them by:
通过以下方式忽略它们:
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