Git 不会忽略 .gitignore 中的某些 Xcode 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3296739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:33:37  来源:igfitidea点击:

Git not ignoring certain Xcode files in .gitignore

xcodegit

提问by agentbanks217

I am new to Git and I am using it to backup an iPhone project I am working on. I have added a list of files that Git should ignore (xcode files) when I update, but this .perspectivev3 (which is in my .gitignore) file keeps showing up when I go to commit my changes. Does anyone know why this is, or what I am doing wrong?

我是 Git 的新手,我正在使用它来备份我正在处理的 iPhone 项目。我添加了更新时 Git 应该忽略的文件列表(xcode 文件),但是当我提交更改时,这个 .perspectivev3(在我的 .gitignore 中)文件一直显示。有谁知道这是为什么,或者我做错了什么?

Thanks,

谢谢,

Zach

扎克

This is what is in my .gitignore file:

这是我的 .gitignore 文件中的内容:

# xcode noise
*.mode1v3
*.pbxuser
*.perspective 
*.perspectivev3
*.pyc 
*~.nib/
build/*

# Textmate - if you build your xcode projects with it
*.tm_build_errors

# old skool
.svn

# osx noise
.DS_Store
profile

回答by VonC

If it keep showing up in the git status, it must have been added or committed before.

如果它一直显示在 git status 中,则它必须之前已添加或提交。

You need to

你需要

  • git rm --cachedthat file, in order for the git statusto not list it anymore (it is was just added, but not committed yet).
  • git rmthat file, if it was previously committed (see this question for instance)
  • git rm --cached该文件,以便git status不再列出它(它刚刚添加,但尚未提交)。
  • git rm该文件,如果之前已提交(例如,请参阅此问题

回答by jpswain

You can use

您可以使用

$ git rm --cached ./whatever1.txt

after something is already under version control.

在某些东西已经在版本控制之下之后。

In fact, if you have "whatever1.txt" under version control and you want to remove it from git, but leave your working tree undisturbed, then just do this:

事实上,如果您在版本控制下有“whatever1.txt”,并且您想从 git 中删除它,但不打扰您的工作树,那么只需执行以下操作:

$ git rm --cached ./whatever1.txt
$ echo /whatever1.txt >> ${PROJECT_ROOT}/.gitignore
$ git status   # this will now show ./whatever1 as "deleted" (from git, not your working tree, and will show gitignore as modified or created)
$ git commit -a

And that's it.

就是这样。

Only use

只使用

$ git rm

when you want to remove the file from both the working tree ANDthe git repo.

当您想从工作树git repo 中删除文件时。

CAVEAT: The likely scenario you would use this is for removing IDE-specific files from git. In this example "whatever1" represents your IDE file(s) you're removing. If you are working on a project with several people and you push this changeset to a shared repo, then their "./whatever1" files WILL BE DELETED when they pull this changeset. The easy thing to do from here for the people on the receiving end is:

警告:您可能会使用它来从 git 中删除特定于 IDE 的文件。在本例中,“whatever1”代表您要删除的 IDE 文件。如果您正在与多个人一起处理一个项目,并且您将此变更集推送到共享存储库,那么他们的“./whatever1”文件将在他们提取此变更集时被删除。对于接收端的人来说,从这里开始做的事情很简单:

$ git checkout 1215ef -- ./file-you-want-to-restore ./another-file ./another-etc

(where 1215ef represents the last commit before the deletion)

(其中 1215ef 代表删除前的最后一次提交)

This has the effect of restoring those files that were present at their last commit before the pull. After they have done this those files will be safe and not show up as uncommitted b/c they will fall under the exclusion of gitignore.

这具有恢复在拉取之前最后一次提交时存在的那些文件的效果。完成此操作后,这些文件将是安全的,不会显示为未提交的 b/c,它们将被排除在 gitignore 之外。

Good luck!

祝你好运!

回答by jA_cOp

.gitignore only applies for untracked files. If you've git-add'ed files that are otherwise untracked due to .gitignore, they will still be part of the repository.

.gitignore 仅适用于未跟踪的文件。如果您添加了 git-add'ed 的文件,这些文件由于 .gitignore 而未被跟踪,它们仍将是存储库的一部分。

Simply remove the files from the repository you don't want anymore:

只需从存储库中删除您不再需要的文件:

git rm *.perspectivev3