简体   繁体   English

Git正在跟踪目录,不会忽略它们

[英]Git is tracking directories and will not ignore them

I must have read at least 50 StackOverflow questions and answers that say that Git cannot track directories. 我必须至少阅读了50条StackOverflow问题和答案,这些问题和答案说Git无法跟踪目录。 And yet, that is exactly what seems to be happening. 但是,这似乎正在发生。

I created a project (.NET, on Windows), and added and committed all the files prior to adding a .gitignore. 我创建了一个项目(在Windows上为.NET),并在添加.gitignore之前添加并提交了所有文件。 Realizing my mistake later on, I git rm -r --cached :/ everything, added this .gitignore , and the re-added and committed my files. 后来意识到我的错误,我git rm -r --cached:/一切,添加了.gitignore ,并重新添加并提交了我的文件。 The thing is, git still tracks my obj and bin folders even though they seem to be ignored in the .gitignore. 问题是,即使.gitignore中的git和bin文件夹似乎被忽略,git仍然跟踪我的obj和bin文件夹。

Here are the relevant lines from the .gitignore: 以下是.gitignore中的相关行:

[Bb]in/
[Oo]bj/
bin/**
obj/**

One or two of those might not make sense, I'm not totally familiar with .gitignore rules and was just trying to see what would stick. 其中一两个可能没有意义,我对.gitignore规则并不完全熟悉,只是想看看会发生什么。

Here's what I get for git status: 这是我对git status的了解:

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

    src/main/dotnet/ETB/ETB.Droid/bin/
    src/main/dotnet/ETB/ETB.Droid/obj/
    src/main/dotnet/ETB/ETB.iOS/bin/
    src/main/dotnet/ETB/ETB.iOS/obj/
    src/main/dotnet/ETB/ETB/bin/
    src/main/dotnet/ETB/ETB/obj/
    src/main/dotnet/packages/

This is even after I do something like git rm -r --cached .\\src\\main\\dotnet\\ETB\\ETB.Droid\\bin from the root level. 即使在我从根级别执行git rm -r --cached。\\ src \\ main \\ dotnet \\ ETB \\ ETB.Droid \\ bin之后,也是如此。 There are also ZERO tracked files from within these directories that appear in the "Changes not staged for commit" section when I do a git status. 当我执行git status时,这些目录中还有零跟踪的文件,这些文件显示在“未暂缓提交的更改”部分中。

I'm really, really stumped. 我真的非常难过。 Can anyone help me figure out why I can't ignore these directories completely? 谁能帮我弄清楚为什么我不能完全忽略这些目录?

Update 更新资料

I made the changes that the commenters suggested, and it seemed to solve some, but not all, of my problems (sorry I had it marked answered for a bit there). 我进行了评论者建议的更改,它似乎解决了部分但不是全部问题(对不起,我在这里稍作回答就对了)。 Relevant lines in my .gitignore at the root level are: 我的.gitignore根目录中的相关行是:

**/[Dd]ebug/**
**/bin/**
**/obj/**

That first line is probably not necessary, but I figured it couldn't hurt. 第一行可能不是必需的,但我认为这不会造成伤害。 There is definitely no extra whitespace on any of these lines. 这些行中绝对没有多余的空格。

For some reason, only one of the obj directories is still showing up in Git. 由于某种原因,Git中仍然只显示一个obj目录。 I even deleted and re-added everything just to try it out. 我什至删除并重新添加了所有内容以进行尝试。

The offending directory is the ETB.Data directory: 令人讨厌的目录是ETB.Data目录:

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

        src/main/dotnet/ETB.Data/

So I ran this command: 所以我运行了这个命令:

git rm -r --cached .\src\main\dotnet\

I then committed those deletes. 然后我提交了这些删除。 Then I tried to re-add the directory 然后我尝试重新添加目录

git add .\src\main\dotnet

When I look at my status, here is what I'm seeing: 当我查看自己的状态时,会看到以下内容:

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   src/main/dotnet/ETB.Data/obj/Debug/TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
        new file:   src/main/dotnet/ETB.Data/obj/Debug/TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
        new file:   src/main/dotnet/ETB.Data/obj/Debug/TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
        new file:   src/main/dotnet/ETB.sln
        ...
        ...

Why do these files keep showing up?! 为什么这些文件一直显示? The obj and bin directories in other project directories are being ignored. 其他项目目录中的obj和bin目录将被忽略。 Does anyone know why this one isn't being ignored? 有谁知道为什么不忽略这一点?

You need to tell git to ignore all the bin / obj files/folders, not just the ones at its root : 您需要告诉git忽略所有bin / obj文件/文件夹,而不仅仅是其根目录下的文件/文件夹:

**/bin/**
**/obj/**

From man gitignore : 来自man gitignore

  • A leading "**" followed by a slash means match in all directories. 前导“ **”后跟斜杠表示在所有目录中均匹配。 For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". 例如,“ ** / foo”与文件“ foo”在任何位置都匹配文件或目录“ foo”。 "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo". “ ** / foo / bar”与文件或目录“ bar”匹配,直接位于目录“ foo”下。

  • A trailing "/**" matches everything inside. 尾部的“ / **”匹配内部的所有内容。 For example, "abc/**" matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth. 例如,相对于.gitignore文件的位置,“ abc / **”匹配目录“ abc”内的所有文件的深度是无限的。

Thats very simple because your line in your .gitignore file are not correct. 那很简单,因为您的.gitignore文件中的行不正确。 I can't test it now but try something like this for example 我现在无法对其进行测试,但可以尝试例如以下操作

**/bin/**
**/obj/**

When you don't write the * at the beginning your line is interpreted as the start. 如果您没有在开头写* ,那么您的行将被解释为开始。

There is a good comment if you read the man page. 如果阅读手册页,则会有很好的评论。

. A leading " " followed by a slash means match in all directories. 前导“ ”后跟斜杠表示在所有目录中均匹配。 For example, " /foo" matches file or directory "foo" anywhere, the same as pattern "foo". 例如,“ / foo”在任何位置都与文件或目录“ foo”匹配,与模式“ foo”相同。 "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo". “ ** / foo / bar”与文件或目录“ bar”匹配,直接位于目录“ foo”下。

. A trailing "/ " matches everything inside. 尾部的“ / ”匹配内部的所有内容。 For example, "abc/ " matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth. 例如,相对于.gitignore文件的位置,“ abc / ”匹配目录“ abc”内的所有文件的深度是无限的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM