简体   繁体   English

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

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

After reading several questions about .gitignore I 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 *.txt in all folders. 我必须添加到.gitignore中以忽略具有某个结尾的所有文件,比如所有文件夹中的*.txt

I'd prefer to only have one .gitignore file at top level. 我更喜欢在顶层只有一个.gitignore文件。

I tried several things but none worked. 我尝试了几件事,但都没有用。

This is what I tested ( .gitignore was 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? 为什么文件内容/ 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 -x but not by clean -n , I think they are ignored. 如果文件content / abc.txt和content / def.txt由clean -n -x而不是clean -n ,我认为它们会被忽略。 But then why don't they show up in git status --ignored --untracked-files=all ? 但那么为什么它们不会出现在git status --ignored --untracked-files=all

Just adding *.txt works. 只需添加*.txt Check gitignore(5) man page for the gitignore format explanation 检查gitignore(5)手册页以获取gitignore格式说明

If you try to add the content directory, all the *.txt file 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

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

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