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文件的干扰了。

相关推荐
触底反弹6 小时前
Vibe Coding 不写 Git,等于悬崖边飙车
人工智能·git·面试
泡沫冰@7 小时前
基于Git、Jenkins、Podman、ECS的CI/CD实践
git·jenkins·podman
zfoo-framework8 小时前
git拦截大于5M文件
git
马里马里奥-11 小时前
VS Code Git 工作树:解锁多分支并行开发新体验
git
七牛云行业应用14 小时前
Ollama 本地部署 DeepSeek 完全指南:macOS / Windows / Linux 三端安装 + GPU 配置 + API 调用
linux·windows·macos
2401_8534482315 小时前
Git安装流程和基础使用步骤
git·github
j7~19 小时前
【Git】《Git 系列指南(一):初识Git和安装Git配置》
运维·git·git版本控制器·git安装步骤
独隅19 小时前
DevEco Code 在 Windows/MacOS 双系统上的完整使用指南
ide·人工智能·windows·macos·华为·harmonyos
白玉cfc19 小时前
【iOS】weak底层原理
macos·ios·cocoa
a44931536220 小时前
iPhone 触控失灵诊断——触控IC与屏幕数字化层故障的芯片级排查
macos·电脑