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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10617801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 06:55:12  来源:igfitidea点击:

ignore certain files in all folders using a single .gitignore file

gitgitignore

提问by Onur

After reading several questions about .gitignoreI have found no one that could answer my question:

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

What do I have to add to .gitignore to ignore all files with a certain ending, say *.txtin all folders.

我必须向 .gitignore 添加什么才能忽略具有特定结尾*.txt的所有文件,例如 在所有文件夹中。

I'd prefer to only have one .gitignorefile at top level.

我宁愿只有一个.gitignore顶级文件。

I tried several things but none worked.

我尝试了几件事,但都没有奏效。

This is what I tested (.gitignorewas added to the repository before, no other file was added):

这是我测试的(.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")

This is as expected: all files show up since .gitignore is empty.

这是预期的:所有文件都显示出来,因为 .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")

Why don't the files content/abc.txt and content/def.txt show up in the list?

为什么文件 content/abc.txt 和 content/def.txt 没有出现在列表中?

  $ git clean -n
  Would not remove content/

I thought they would show up here too.

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

  $ 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

If the files content/abc.txt and content/def.txt are shown by clean -n -xbut not by clean -n, I think they are ignored. But then why don't they show up in git status --ignored --untracked-files=all?

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

采纳答案by KurzedMetal

Just adding *.txtworks. Check gitignore(5) man pagefor the gitignore format explanation

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

If you try to add the contentdirectory, all the *.txtfile will be ignored.

如果您尝试添加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