Git 中忽略 Mac 生成的 .DS_Store文件

要在 Git 中忽略 Mac 生成的 .DS_Store文件,你可以按照以下步骤操作:

1. 创建或编辑 .gitignore文件

在你的项目根目录下,创建或编辑一个名为 .gitignore的文件:

复制代码
touch .gitignore

2. 添加 .DS_Store.gitignore

打开 .gitignore文件,添加以下内容:

复制代码
.DS_Store

如果你希望忽略所有目录中的 .DS_Store文件(包括子目录),可以添加:

复制代码
**/.DS_Store

3. 全局忽略 .DS_Store(可选)

如果你想在所有 Git 项目中忽略 .DS_Store,可以设置全局 .gitignore

复制代码
echo .DS_Store >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

4. 删除已跟踪的 .DS_Store文件

如果 .DS_Store已经被 Git 跟踪,你需要先将其从 Git 中删除:

复制代码
git rm --cached .DS_Store

如果要删除所有已跟踪的 .DS_Store文件:

复制代码
find . -name .DS_Store -print0 | xargs -0 git rm --cached

5. 提交更改

最后,提交 .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文件的干扰了。

相关推荐
10086love1001010 小时前
Macos安装codex
macos
m0_5791466510 小时前
已被 Git 追踪的本地修改文件如何实现临时忽略
git
糖少主13 小时前
WSL中使用Beyond Compare 3/4/5作为difftool
git·wsl·beyond compare·difftool
console.log('npc')15 小时前
Git版本管控:git reset \+ git push \-f 原理、实操与避坑指南
git
ryanuo716 小时前
Mac(M芯片)上进行嵌入式开发遇到的问题
嵌入式硬件·macos·开发板
不爱记笔记17 小时前
苹果WWDC 2026全解析:Apple Intelligence+ 性能提升数据一览
macos·ios·wwdc
恋喵大鲤鱼20 小时前
git reflog
git·git reflog
MatrixOrigin21 小时前
MatrixOne Git4Data 技术详解(二):从零跑通所有 Git 原语
git
小小程序员mono21 小时前
WWDC 2026 之后,M5 桌面 Mac 上市时间预测
macos·ios·wwdc
anew___21 小时前
常用的 Git 工作流
git