我已将 .DS_Store 添加到 .gitignore 文件中,但似乎它只是忽略了根目录中的 .DS_Store,而不是每个文件夹和子文件夹中的。
我该如何解决?
我认为您遇到的问题是在之前的一些提交中,您不小心将 .DS_Store
文件添加到了存储库中。当然,一旦在您的存储库中跟踪文件,即使它与适用的 .gitignore 文件中的条目匹配,它也会继续被跟踪。
您必须手动删除已添加到您的存储库的 .DS_Store
文件。您可以使用
git rm --cached .DS_Store
一旦删除,git 应该忽略它。您只需要在根 .gitignore
文件中添加以下行:.DS_Store
。不要忘记期间!
git rm --cached .DS_Store
从当前目录中仅删除 .DS_Store
。您可以使用
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
从存储库中删除所有 .DS_Stores
。
提示:由于您可能永远不想包含 .DS_Store
个文件,因此请制定一个全局规则。首先,在某处创建一个全局 .gitignore
文件,例如
echo .DS_Store >> ~/.gitignore_global
现在告诉 git 将它用于所有存储库:
git config --global core.excludesfile ~/.gitignore_global
This page 帮助我回答了您的问题。
将**/.DS_Store
添加到子目录的 .gitignore
中
如果 .DS_Store
已提交:
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
在所有存储库中忽略它们:(有时它命名为 ._.DS_Store
)
echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
git rm --cached your_file
find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch
,来自:stackoverflow.com/questions/18393498/…
-f
:find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
如果 .DS_Store 从未添加到您的 git 存储库中,只需将其添加到您的 .gitignore 文件中。
如果没有,请创建一个名为
.gitignore
在您的应用程序的根目录中,只需编写
**/.DS_Store
在里面。这绝不会允许 .DS_Store 文件潜入您的 git。
但是,如果它已经存在,请在终端中写入:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
然后提交并推送更改以从远程存储库中删除 .DS_Store:
git commit -m "Remove .DS_Store from everywhere"
git push origin master
现在将 .DS_Store 添加到您的 .gitignore 文件中,然后再次提交并使用最后两段代码进行推送(git commit ...,git push ...)
*
或 **
取决于您想要实现的目标。这是一种告诉 shell 如何导航目录的方法。这个 stackoverflow 答案完全解释了它stackoverflow.com/a/28199633/4092170
您的 .gitignore 文件应如下所示:
# Ignore Mac DS_Store files
.DS_Store
只要您不包含斜杠,它就会与所有目录中的文件名匹配。 (来自 here)
git rm --cached path/to/file/here
只需将其放在 .gitignore
中的新行
**/.DS_Store
前导“**”后跟斜杠表示在所有目录中都匹配。例如,“**/foo”匹配任何地方的文件或目录“foo”,与模式“foo”相同。 "**/foo/bar" 匹配文件或目录 "bar" 直接位于目录 "foo" 下的任何位置。
**/.DS_Store
只是一种更复杂的书写方式 .DS_Store
。当您想要 gitignore 特定路径模式时,您只需要双星,无论它发生在哪里。例如,只有在 foo
子目录中找到 .DS_Store
文件时,**/foo/.DS_Store
才会隐藏(不管 foo
在哪里)。
第 1 步,删除所有 *.DS_store
文件。一个可以跑
git rm -f *.DS_Store
但请注意,如果您有错字,rm -f
可能会有点危险!第二步:添加
*.DS_Store
.DS_Store
到.gitignore。这对我有用!
将 *.DS_Store
添加到您的 .gitignore 文件中。这对我来说非常有用
您还可以将 --cached
标志添加到 auco 的答案以维护本地 .DS_store 文件,正如 Edward Newell 在他的原始答案中提到的那样。修改后的命令如下所示: find . -name .DS_Store -print0 | xargs -0 git rm --cached --ignore-unmatch
.. 干杯,谢谢!
步骤:1)使用此命令删除现有文件
寻找 。 -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
步骤:2)在您的 .gitignore 文件中添加 .DS_Store
步骤 :3) 在 .gitignore git add .gitignore git commit -m "removed .DS_Store" 中提交您的更改
git rm ./*.DS_Store - 从 git 中删除所有 .DS_Store $ echo \.DS_Store >> .gitignore - 将来忽略 .DS_Store
提交和推送
您应该在创建项目时添加以下行。它将始终忽略要推送到存储库的 .DS_Store
。
*.DS_Store
这将在代码提交时忽略 .DS_Store。
git rm --cached .DS_Store
这是从存储库中删除 .DS_Store 文件,如果需要,可以取消注释。
## ignore .DS_Store file.
# git rm --cached .DS_Store
*.DS_Store