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

相关推荐
猫头虎20 分钟前
如何解决 OpenClaw “Pairing required” 报错:两种官方解决方案详解
网络·windows·网络协议·macos·智能路由器·pip·scipy
春日见6 小时前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
stevenzqzq1 天前
git 常用操作
大数据·git
皮卡车厘子1 天前
Mac 挂载目录
macos
Curvatureflight1 天前
Git工作流最佳实践:从混乱到优雅
git
wu~9701 天前
GitHub永不遗忘,使用git push -f来覆盖的提交依旧保留
git·github
Vermouth_001 天前
git clone的时候提示access denied
git
良逍Ai出海1 天前
在 Windows & macOS 上安装 Claude Code,并使用第三方 Key 的完整教程
windows·macos
热爱生活的五柒1 天前
linux/mac/wsl如何使用claude code,并配置免费的硅基流动API?(官方的需要付费订阅)
linux·运维·macos
qq_437657271 天前
清楚本地的git并重新登录
git