4

Git 在 macOS 中忽略所有 .DS_Store 文件

 2 years ago
source link: https://blog.ichr.me/post/git-ignore-all-ds-store-on-mac/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

macOS 每个文件夹都存放着一个 .DS_Store 文件,是用于存放目录自定义属性(如图表、位置属性)等元数据信息的系统文件,由 Finder 自动创建。虽然所有 . 开头的文件/文件夹默认隐藏(可以使用 Command + Shift + . 显示所有隐藏文件),平时我们看不见,也不影响使用,但是 Git 仍会将其记录下来,即便我只是在同目录下移动文件。所以为了防止某些信息不经意间泄露出来,建议使用某些手段使 Git 忽略 .DS_Store 文件。

.gitignore

.gitignore 是 Git 提供的一个让用户控制 Git 忽略某些文件的文件。所有被 .gitignore 匹配的文件都不会被 Git 记录。

我们可以在仓库的根目录下新建一个 .gitignore 文件,让后追加以下内容:

.DS_Store
**/.DS_Store
.DS_Store?

这样依赖这个仓库就不会再记录 .DS_Store 文件了。

虽说可以使用上面的方法来忽略 .DS_Store ,但是每个仓库都要单独配置一遍,不如全局忽略来得直接了当。

  1. 打开终端,在某一个位置创建 .gitignore_global 文件(建议在当前用户目录下)

    touch ~/.gitignore_global
  2. 修改该文件,填入与上方 .gitignore 示例一样的内容

    .DS_Store
    **/.DS_Store
    .DS_Store?
  3. 对 Git 进行全局配置

    git config --global core.excludesfile ~/.gitignore_global

此时终端输入:

git config --list

如果发现这两行:

[core]
	excludesfile = /Users/[username]/.gitignore_global

就说明已经添加成功了,以后 Git 就不会再记录 .DS_Store 了。

批量移除已有的 .DS_Store

即便后续不会再记录,仓库中的 .DS_Store 都还在,需要手动删除。

终端进入仓库目录,输入:

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

这样就删除了所有该仓库的 .DS_Store 。重新提交推送即可。


参考链接


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK