在阅读了几个关于.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")
这是预期的:所有文件都显示,因为 .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")
为什么文件 content/abc.txt 和 content/def.txt 不显示在列表中?
$ git clean -n
Would not remove content/
我以为他们也会出现在这里。
$ 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
如果文件内容/abc.txt和content/def.txt由clean -n -x
显示,而不是由clean -n
显示,我认为它们被忽略了。但是为什么他们不出现在git status --ignored --untracked-files=all
?
只需添加*.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