.gitignore没有忽略.idea路径

194

我需要做什么才能让git忽略我的.idea/文件夹?

ctote@ubuntu:~/dev/1$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   .idea/.name
    modified:   .idea/misc.xml
    modified:   .idea/modules.xml
    modified:   .idea/vcs.xml
    modified:   .idea/workspace.xml
    modified:   src/Receiver.java
    modified:   test/1/agent/WindowsQueryHandlerTest.java

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    lib/
    mp1.iml

no changes added to commit (use "git add" and/or "git commit -a")

ctote@ubuntu:~/dev/1$ cat .gitignore
*.class

# Package Files #
*.war
*.ear

# IDEA config files
.idea/

2
可能是忽略已经提交到Git存储库的文件的重复问题。 - jub0bs
只需忽略文件即可 - 任何文件夹或文件都可以被忽略 - 进入Intellij工具,右键单击文件/文件夹 --> git --> 添加到.gitignore文件中,就这样 - 从那时起,Intellij将开始忽略这些文件/文件夹的更改。 - Ashish Shetkar
13个回答

489

.gitignore 仅忽略新添加的(未跟踪的)文件。

如果您有已经添加到仓库中的文件,则它们的所有更改将像往常一样被跟踪,即使它们与 .gitignore 规则匹配。

要从仓库中删除该文件夹(而不从磁盘中删除),请执行:

git rm --cached -r .idea

21
致命错误:路径规范'.idea'未找到任何文件。 - Oliver Dixon
我也收到了上述的“pathspec...”错误信息。 - Solx85
6
我确认了您的建议,我也遇到了与上面提到的相同的错误,但是我使用"-f"强制删除了它。谢谢。 - Srinivas Damam
我已经寻找这个很长时间了。可行。谢谢! - CodeGodie
2
这句话非常有帮助:".gitignore只会忽略新添加的(未跟踪的)文件。" 谢谢!现在我意识到我的同事提交了一些让我很烦恼的临时文件!我只需要使用git rm命令将它们删除并提交即可。 - AlexD
显示剩余3条评论

52

.idea/添加到.gitignore文件中

在终端中运行以下命令以完成任务 :)

git rm -rf .idea
git commit -m "delete .idea"
git push

3
致命错误:路径规范'.idea'与任何文件不匹配。 - Martin Erlic
1
@MartinErlic 请先cd到项目根目录,然后运行这些命令。 - Hamid Zandi

26

要删除"fatal: pathspec '.idea' did not match any files"错误,可以使用以下命令(如果目录仍然未被跟踪):

git clean -f -d .idea


6
这是我尝试过的所有方法中唯一有效的解决方案。 - Josh Desmond
8
这将从您的文件系统中删除该文件夹! - Pero122

19

从代码库中移除该文件,请运行以下命令

git rm --cached .idea/

现在更新你的 .gitignore 文件以忽略 .idea 文件夹,请运行以下命令。

echo '.idea' >> .gitignore

添加.gitignore文件

git add .gitignore

git commit -m "Removed .idea files"

从 git rm -r --cached .idea/ 开始,以避免/解决“fatal: not removing '.idea/' recursively without -r”错误。 - Onat Korucu
对于像我一样使用IntelliJ Community Edition或Android Studio的人来说,做任何事情都要在IDE之外使用类似Git Bash shell的命令行完成,包括"git commit …"。只有在这之后,已经添加到.idea文件夹中的文件才会从IntelliJ的提交对话框中消失。 - undefined

10

对于那些收到fatal: pathspec '.idea' did not match any files错误提示的人:

你只需要包含完整的.idea文件夹路径即可。

因此,首先执行git status命令,它应该会显示您当前所在位置的.idea路径。

然后,在w0lf建议的命令中包括路径:git rm --cached -r example/path/to/.idea


8

如果在按照其他答案的步骤之后,你发现你的git实例在bash或powershell中仍然持续跟踪.idea/文件夹,那么请尝试以下方法!

我发现Jet Brains IDE有它们自己的.gitignore,并运行自己的git实例将覆盖你的.gitignore设置!

我解决这个问题的方法是导航到/.idea/.gitignore,并将其内容更改为:

/**

为了忽略文件夹中的所有内容。

下面的图片展示了如何在Jetbrains IDE中导航到.gitignore文件。

定位.gitignore


这是对我有效的方法。我没有提交.idea文件夹。 - Manuel
这是有效的解决方案。即使运行了 git clean 并将 .idea 文件夹添加到 .gitignore 中,文件仍会被添加回来。我猜测这是由于两个 .gitignore 文件之间的冲突引起的?这个解决方案解决了这个问题。 - veesar

3

尽管上面提到了很多好的解决方案,但是对于我来说都不起作用,因为我犯了一个错误,就是在项目创建时忽略了这些设置!

  1. 有两个.gitignore文件我没有注意并更新项目级别的文件。
  1. 既然我已经在项目创建时提交了那些文件/路径,每次构建命令后都会与远程存在差异;尽管它们已添加到.gitignore中。

解决方案

  • 我关注了第二个问题,并首先从远程删除了这些文件夹/目录。
  • 本地拉取。
  • 终于!不再跟踪被忽略的文件!

3
  1. 修改 .gitignore 文件并保存。
  2. 以管理员身份打开 CMD 或终端并进入项目文件夹。
  3. 运行 git rm -r --cached .idea/
  4. 运行 git add .
  5. 运行 git commit -m "chore: Rider files ignored."

详情请参见如何修复 gitignore

在我的情况下,我的 .gitignore 文件包含以下内容:

### Rider ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml
.idea/.idea.TemplateProject/.idea/indexLayout.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

你是对的,但不要像“缓存刷新”一样应用“git rm -r --cached <directory>”。这在某些情况下可能是危险的。 - sopehl
你能提供一个例子,说明这会引起问题吗? - Onat Korucu

1
在输入以上命令后出现错误"fatal: pathspec '.idea' did not match any files"的解决方法:
  1. 检查您的idea文件夹及其文件的路径。
  2. 使用git status命令进行检查。它将像往常一样列出所有文件。检查.idea文件夹文件的路径。我的路径是../.idea/workspace.xml。注意../.idea
  3. 修改接受答案中上述建议的命令为git rm --cached -r ../.idea
  4. 然后您将看到这个rm '.idea/workspace.xml'并且文件将被删除。

@AlexTotheroh 是的,看起来是一样的。我只是试图通过分享我在自己项目中解决这个问题所采取的精确步骤来帮助其他人。 - Gautam Mandsorwale
我这里也有同样的问题。.gitignore在模块目录中而不是项目目录中。我尝试了../.idea,但它没有起作用。它仍然建议我跟踪.idea内部的文件。 - KareemJ

1

为了忽略.idea文件夹和iml文件类型,我执行了以下操作:

enter image description here


是的,将.gitignore移动到我的项目根目录下拯救了我的一天。 - ShadowGames

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接