使用单个.gitignore文件忽略所有文件夹中的某些文件

Onu*_*nur 7 git gitignore

在阅读了几个问题后,.gitignore我发现没有人可以回答我的问题:

我必须添加到.gitignore以忽略具有特定结尾*.txt的所有文件,例如 在所有文件夹中.

我更喜欢只有一个.gitignore顶级文件.

我尝试了几件事,但都没有用.

这是我测试的(.gitignore之前添加到存储库,没有添加其他文件):

  $ ls
  abc.txt  content

  $ ls content/
  abc.txt  def.other  def.txt

  $ echo "" > .gitignore


  $ git status --ignored --untracked-files=all
  # 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)
  #
  #       abc.txt
  #       content/abc.txt
  #       content/def.other
  #       content/def.txt
  no changes added to commit (use "git add" and/or "git commit -a")
Run Code Online (Sandbox Code Playgroud)

这是预期的:自.gitignore为空以来所有文件都显示出来.

  $ echo "*.txt" > .gitignore

  $ git status --ignored --untracked-files=all
  # 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)
  #
  #       content/def.other
  # Ignored files:
  #   (use "git add -f <file>..." to include in what will be committed)
  #
  #       abc.txt
  no changes added to commit (use "git add" and/or "git commit -a")
Run Code Online (Sandbox Code Playgroud)

为什么文件内容/ abc.txt和content/def.txt不显示在列表中?

  $ git clean -n
  Would not remove content/
Run Code Online (Sandbox Code Playgroud)

我以为他们也会出现在这里.

  $ echo "" > .gitignore

  $ git clean -n
  Would remove abc.txt
  Would not remove content/

  $ cd content

  $ git clean -n -x
  Would remove def.other

  $ git clean -n -x
  Would remove abc.txt
  Would remove def.other
  Would remove def.txt
Run Code Online (Sandbox Code Playgroud)

如果文件content/abc.txt和content/def.txt显示clean -n -x但不显示clean -n,我认为它们会被忽略.但那他们为什么不出现git status --ignored --untracked-files=all呢?

Kur*_*tal 3

只是添加*.txt作品。检查gitignore(5) 手册页以获取 gitignore 格式说明

如果尝试添加content目录,所有*.txt文件都将被忽略。

$ echo "*.txt" > .gitignore 

$ git add content

$ git status --ignored --untracked-files=all
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   content/def.other
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
# Ignored files:
#   (use "git add -f <file>..." to include in what will be committed)
#
#       abc.txt
#       content/abc.txt
#       content/def.txt
Run Code Online (Sandbox Code Playgroud)