【问题标题】:git status shows files in untracked list though they are in .gitignore and removed from the cachegit status 显示未跟踪列表中的文件,尽管它们位于 .gitignore 中并从缓存中删除
【发布时间】:2014-09-13 03:03:40
【问题描述】:
~/rails_projects/sample_app2 $ git branch
* master

~/rails_projects/sample_app2$ cat .gitignore
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
#   git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

# Ignore other unneeded files.
database.yml
doc/
.*.s[a-w][a-z]  #all swap files
.*.*.s[a-w][a-z]
.*.*.*.s[a-w][a-z]
*~
.project
.DS_Store
.idea
.secret

~/rails_projects/sample_app2$ touch .gitignore.swp

~/rails_projects/sample_app2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 15 commits.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore.swp
nothing added to commit but untracked files present (use "git add" to track)

~/rails_projects/sample_app2$ git add .
~/rails_projects/sample_app2$ git commit -m "Add swap file"
[master 364570c] Add swap file
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore.swp

~/rails_projects/sample_app2$ git rm --cached .gitignore.swp
rm '.gitignore.swp'

~/rails_projects/sample_app2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 16 commits.
#
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    .gitignore.swp
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore.swp


~/rails_projects/sample_app2$ git commit -m "Remove swap file"
[master 485217f] Remove swap file
 0 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 .gitignore.swp

~/rails_projects/sample_app2$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 17 commits.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore.swp
nothing added to commit but untracked files present (use "git add" to track)

~/rails_projects/sample_app2$ $ git add .

~/rails_projects/sample_app2$ git commit -m "Trying NOT to add swap files"
[master d743282] Trying NOT to add swap files
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore.swp

不断地不断。当我这样做时,交换文件一直显示在未跟踪的文件列表中:

$ git status

因此,当我进行下一次提交时,交换文件会添加到 git 正在跟踪的文件中。如何让 git 忽略所有交换文件?

这似乎是一个常见的问题,应该有一个命令:

$ git ignore --all-swap-files

对其中一个 cmets 的回应:

~/rails_projects$ mkdir test_gitignore
~/rails_projects$ cd test_gitignore/
~/rails_projects/test_gitignore$ touch .gitignore
~/rails_projects/test_gitignore$ echo '.*.s[a-w][a-z]' > .gitignore
~/rails_projects/test_gitignore$ git init
Initialized empty Git repository in /Users/7stud/rails_projects/test_gitignore/.git/
~/rails_projects/test_gitignore$ touch file1.txt .file1.swp
~/rails_projects/test_gitignore$ mkdir subdir
~/rails_projects/test_gitignore$ touch subdir/.file2.swp
~/rails_projects/test_gitignore$ ls -al
total 8
drwxr-xr-x   7 7stud  staff  238 Sep 14 10:41 .
drwxr-xr-x  19 7stud  staff  646 Sep 14 10:38 ..
-rw-r--r--   1 7stud  staff    0 Sep 14 10:40 .file1.swp
drwxr-xr-x  10 7stud  staff  340 Sep 14 10:40 .git
-rw-r--r--   1 7stud  staff   15 Sep 14 10:40 .gitignore
-rw-r--r--   1 7stud  staff    0 Sep 14 10:40 file1.txt
drwxr-xr-x   3 7stud  staff  102 Sep 14 10:41 subdir
~/rails_projects/test_gitignore$ ls -al subdir
total 0
drwxr-xr-x  3 7stud  staff  102 Sep 14 10:41 .
drwxr-xr-x  7 7stud  staff  238 Sep 14 10:41 ..
-rw-r--r--  1 7stud  staff    0 Sep 14 10:41 .file2.swp

~/rails_projects/test_gitignore$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   file1.txt
nothing added to commit but untracked files present (use "git add" to track)
~/rails_projects/test_gitignore$ 

【问题讨论】:

  • @meager,您花了多少时间阅读“重复”并将答案与我正在做的事情进行比较?
  • 不长,很明显。
  • 我已经回滚了这个问题,因为您已经对其进行了重大编辑(特别是,我的答案中建议的更改已被合并,其他内容也已更改)。请ask a new question - 别担心,问题是免费的,所以你可以(在合理的范围内)想问多少就问多少!
  • 是的,我试图证明您的解决方案不起作用。

标签: git


【解决方案1】:

这是正在发生的事情:您的 .gitignore 规则末尾有多余的空格。 Git 将 gitignore 中的每个条目完全视为忽略规则,这正是出于同样的原因,您不应该在 gitignore 中包含该注释 - 模式会发生变化。

所以,.*.s[a-w][a-z],虽然在您的 .gitignore 中看起来没问题,但恰好是字符串 ".*.s[a-w][a-z] "

因此 git ignore 无法匹配交换文件。

顺便说一句,您可以取消三个交换 ingore 规则并改用单个规则

*.s[a-w][a-z]

这里有一个示例 python sn-p 来测试新模式到底匹配(或不匹配)

In [1]: import fnmatch

In [2]: pattern = "*.s[a-w][a-z]"

In [3]: filenames = [".file.swp", "folder/.file.swp", "folder/subfolder/.file.swp", ".f.i.l.e.swp", "folder/.f.i.l.e.swp"]

In [4]: for filename in filenames:
   ...:     print filename, fnmatch.fnmatch(filename, pattern)
   ...:     
.file.swp True
folder/.file.swp True
folder/subfolder/.file.swp True
.f.i.l.e.swp True
folder/.f.i.l.e.swp True

In [5]: pattern = ".*.s[a-w][a-z]"

In [6]: for filename in filenames:
    print filename, fnmatch.fnmatch(filename, pattern)
   ...:     
.file.swp True
folder/.file.swp False
folder/subfolder/.file.swp False
.f.i.l.e.swp True
folder/.f.i.l.e.swp False

【讨论】:

  • 感谢您克服所有噪音并帮助我解决问题。在我的第一个模式之后有两个空格。你应该得到比我允许奖励你更多的分数。不幸的是,在 SO 上,简单、愚蠢的答案在 5 分钟内获得 60 分,但对于棘手问题的答案却从来没有得到太多的赞誉。
  • 您可以取消三个交换 ingore 规则并使用单个规则来代替 它们并不等效,我相信在进一步思考后您会意识到这一点。这个问题之前在另一个答案下讨论过,但是 falsetru 删除了他们的答案。但是在我的 cmets 中,我发布了来自我发现的来源的引用,“没有 glob 模式将匹配一个句点。你必须明确指定一个句点。”,你非常好的 python 示例和我的测试显示显然是错误的。因此,如果 glob 是贪婪的,那么我只需要 .*.s[a-w][a-z]
  • 我的测试表明,在 python 和 bash shell 中,* 是贪婪的。
  • @7stud 是的,我知道它们不相等 - 我猜删除的讨论丢失了,所以绝对不知道这是你这样做的原因,因此想简化一下你的通知。无论如何,很高兴知道它已解决:)
  • @7stud 刚刚考虑了您在 cmets 中提到的模式,在某些情况下它可能会失败,但我不确定您是否想首先忽略它们(忽略子目录中的交换文件)。检查我的答案中的编辑。
【解决方案2】:

问题很简单,就是这条线

.*.s[a-w][a-z]  #all swap files

.gitignore 文件中,cmets 只能位于行首。将其更改为

#all swap files
.*.s[a-w][a-z]

它会按预期工作:

$ /bin/echo -e '.*.s[a-w][a-z] # swap' > .gitignore 
$ touch .gitignore.swp
$ touch .x.swp
$ git status
On branch 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:   .gitignore

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

        .gitignore.swp
        .x.swp

no changes added to commit (use "git add" and/or "git commit -a")
$ /bin/echo -e '.*.s[a-w][a-z]\n# swap' > .gitignore 
$ git status
On branch 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:   .gitignore

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

【讨论】:

  • @7stud 首先,请保持礼貌。你为什么这么粗鲁?其次,请不要编辑您的问题以反映答案中的变化,这使得很难追踪问题。我添加了一个演示问题的 shell 会话的输出。
  • 我的 .gitignore 在你说的地方没有评论。停止编辑我的问题。
  • @7stud 确实如此,在 your question before you edited it 的第 25 行。
  • 您说问题是我的 glob 以句号开头--然后您删除了被否决的答案。 如果您可以编辑/删除您的答案,我可以编辑我的问题.
  • 拜托,在您做任何其他事情之前,请退后一步,重新阅读您所写的内容。您的表现过于激进和令人讨厌,我相信这并不代表您作为 stackoverflow 用户。第二个答案已被删除。通过sufficient privileges 可见。 Here is a screenshot
猜你喜欢
  • 2019-10-30
  • 1970-01-01
  • 2011-06-04
  • 2015-12-04
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 2018-02-18
  • 2021-01-29
相关资源
最近更新 更多