Git 在 macOS 中忽略所有 .DS_Store 文件
前言
- macOS 每个文件夹都存放着一个
.DS_Store文件,是用于存放目录自定义属性(如图表、位置属性)等元数据信息的系统文件,由Finder自动创建。
虽然所有 . 开头的文件/文件夹默认隐藏(可以使用 Command + Shift+。 显示所有隐藏文件),.DS_Store却并不展示,需要通过终端ls -la命令列出,但是 Git 仍会将其记录下来,即便我只是在同目录下移动文件。为了避免 .DS_Store等文件提交到仓库中,通常选择忽略 .DS_Store 文件。
项目中
.gitignore 是 Git 提供的一个让用户控制 Git 忽略某些文件的文件。所有被 .gitignore 匹配的文件都不会被 Git 记录。
在仓库的根目录下创建一个 .gitignore 文件,添加以下内容
这样依赖这个仓库就不会再记录 .DS_Store 文件了。
如果远程仓库已经存在.DS_Store话,可以通过以下命令删除。
sh
git rm --cached */.DS_Store
// 或者
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch这样就删除了所有该仓库的 .DS_Store 。重新提交推送即可。
全局忽略
虽说可以使用上面的方法来忽略 .DS_Store ,但是每个仓库都要配置一遍,不如全局忽略来得方便。
Finder根目录下打开终端,运行以下命令创建.gitignore_global文件
sh
touch ~/.gitignore_global- 通过编辑器修改该文件,添加以下内容。
sh
*~
*.DS_Store
.DS_Store
.DS_Store?
**/.DS_Store
node_modules
**/node_modules
ehthumbs.db
Thumbs.db
# local env files
package-lock.json
package-lock.json?
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?- 使
.gitignore_global生效
sh
git config --global core.excludesfile ~/.gitignore_global- 验证配置是否生效
sh
git config --list或者查看根目录下.gitignore文件
sh
[core]
excludesfile = /Users/[username]/.gitignore_global就说明已经添加成功了,以后 Git 就不会再记录 .DS_Store。
针对网络磁盘
禁止生成 .DS_store
打开 “终端” ,复制黏贴下面的命令,回车执行,然后退出登录 macOS 账户并重新登录。
sh
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE恢复生成.DS_store
sh
defaults delete com.apple.desktopservices DSDontWriteNetworkStores以上命令只是针对网络磁盘,想要阻止本地磁盘中 DS_Store 文件的自动生成,唯一的方式就是停止使用「访达」,我觉得没必要有点以小失大。
参考链接: