Git 中忽略 Mac 生成的 .DS_Store文件
要在 Git 中忽略 Mac 生成的 .DS_Store文件,你可以按照以下步骤操作:
1. 创建或编辑 .gitignore文件
在你的项目根目录下,创建或编辑一个名为 .gitignore的文件:
touch .gitignore2. 添加 .DS_Store到 .gitignore
打开 .gitignore文件,添加以下内容:
.DS_Store如果你希望忽略所有目录中的 .DS_Store文件(包括子目录),可以添加:
**/.DS_Store3. 全局忽略 .DS_Store(可选)
如果你想在所有 Git 项目中忽略 .DS_Store,可以设置全局 .gitignore:
echo .DS_Store >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global4. 删除已跟踪的 .DS_Store文件
如果 .DS_Store已经被 Git 跟踪,你需要先将其从 Git 中删除:
git rm --cached .DS_Store如果要删除所有已跟踪的 .DS_Store文件:
find . -name .DS_Store -print0 | xargs -0 git rm --cached5. 提交更改
最后,提交 .gitignore文件的更改:
git add .gitignore
git commit -m "Ignore .DS_Store files"额外提示
如果你使用的是 macOS,可以考虑在终端中运行以下命令,禁止
.DS_Store文件在网络卷上生成:defaults write com.apple.desktopservices DSDontWriteNetworkStores true然后重启 Finder:
killall Finder
这样,你的 Git 项目就不会再受到 .DS_Store文件的干扰了。
