在macOS平台下会自动生成.DS_Store
文件,在使用git提交的过程中,会发现git将.DS_Store
文件一并提交了,这是我们不需要的。我们可以在项目中新建一个.gitignore
文件,将.DS_Store
添加进去,但是这种方式只对当前项目有效,新建项目之后仍会出现上述问题,所以这里介绍全局忽略.DS_Store
的方法。
- 在
home
目录下新建.gitignore_global
文件,文件内容如下# .gitignore_global .DS_Store .DS_Store?
- 编辑在
home
目录下的.gitconfig
文件,使其引入.gitignore_global
的设置。.gitconfig
的内容如下:[user] name = yourname email = yourname@github.com [core] excludesfile = /Users/yourname/.gitignore_global
home路径中的yourname和自己的对应
- 如果项目中已经出现了
.DS_Store
文件并且已经提交了,我们需要将.DS_Store
文件删除,并再次提交。
- 删除项目中的所有.DS_Store。这会跳过不在项目中的 .DS_Store
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
- 如果要删除目录及其子目录下的
.DS_Store
,执行以下指令find . -name '*.DS_Store' -type f -delete
- 更新项目
git add --all git commit -m '.DS_Store banished!'
- 当然也可以在终端通过修改系统设置来禁止生成
.DS_Store
(好像不能禁止本地生成)。
- 禁止
.DS_Store
生成:defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool TRUE
- 恢复
.DS_Store
生成
参考博客:defaults delete com.apple.desktopservices DSDontWriteNetworkStores