.gitignore不会忽略子目录中的文件

Jal*_*alo 4 git gitignore

我的存储库顶层文件夹中有一个.gitignore文件,其中包含以下代码:

# Compiled python modules.
*.pyc

# Backup gedit files.
/*~

# Setuptools distribution folder.
/dist/

# Python egg metadata, regenerated from source files by setuptools.
/*.egg-info
/*.egg
Run Code Online (Sandbox Code Playgroud)

当我尝试运行git add .命令时,.gitignore无效,因为添加了多个/ *〜。的输出git status如下:

new file:   uvispace/tests/test_messenger.py
new file:   uvispace/tests/test_messenger.py~
new file:   uvispace/tests/test_robot.py~
new file:   uvispace/uvirobot/__main__.py~
new file:   uvispace/uvirobot/messenger.py~
new file:   uvispace/uvirobot/move_base.py
Run Code Online (Sandbox Code Playgroud)

类似问题

我见过几个非常类似的问题,像这一个,或这一个。他们的解决方案是删除先前添加的文件,然后按照以下说明再次添加整个存储库:

git rm -rf --cached .
git add *
Run Code Online (Sandbox Code Playgroud)

但是,我尝试过,没有区别。有人知道为什么会这样吗?

Nik*_*s P 5

有关gitignore语法的文档可在以下位置找到:git-scm.com/docs/gitignore

一件可能是错误的事情是您的,/*~因为单曲*无法按您期望的方式工作:

例如,“ Documentation / *。html”匹配“ Documentation / git.html”,但不匹配“ Documentation / ppc / ppc.html”或“ tools / perf / Documentation / perf.html”。

您必须改为使用**

•前导“ **”后跟斜杠表示在所有目录中均匹配。例如,“ **/foo”与文件“ foo”相同的任何位置都匹配文件或目录“ foo”。“ **/foo/barbar会在目录“ foo” 正下方的任何位置匹配文件或目录“ ”。

•结尾的“ /**”匹配内部的所有内容。例如,“ abc/**”相abc对于.gitignore文件的位置,以无限深度匹配目录“ ”中的所有文件。

•斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,“ a/**/b”匹配“ a/b”,“ a/x/b”,“ a/x/y/b”等。