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

相关推荐
啥都不懂的小小白2 小时前
Git 入门指南:从零开始掌握版本控制
git
AI逐月3 小时前
Git 彻底清除历史记录
大数据·git·elasticsearch
有什么东东4 小时前
Windows安装git教程以及初步使用
git
不爱吃米饭_4 小时前
Gitea 轻量级的Git方案 - Gitlab的替代品
git·gitlab·gitea
CoderJia程序员甲7 小时前
GitHub 热榜项目 - 日榜(2025-12-15)
git·ai·开源·llm·github
大柏怎么被偷了7 小时前
【Git】远程操作
git
studytosky8 小时前
Linux 基础开发工具(3):Git 控制与 GDB 调试实用指南
linux·运维·服务器·网络·数据库·git
云闲不收9 小时前
AI编程系列——git-worktree并行开发
git·ai编程
秦时明月天明9 小时前
GitLab SSH Key 过期:git pull failed : remote your ssh key has expired
git·ssh·gitlab
2501_9151063210 小时前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone