gitignore不工作 - 想要忽略除gitignore文件以外的所有内容以保留"空"目录

Hsi*_*Dai 3 git gitignore

.gitignore在几个目录中有一个文件(例如log/),其中包含以下内容:

*            " Ignore everything in this directory
!.gitignore  " Except this file
Run Code Online (Sandbox Code Playgroud)

尽管如此,如果该目录中有一个文件,git status则向我显示它是新的但未跟踪.

# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       log/blah.log
nothing added to commit but untracked files present (use "git add" to track)
Run Code Online (Sandbox Code Playgroud)

我发现stackoverflow上的其他帖子建议在第一行和各种其他形式或随机伏都教上发表评论,但没有任何效果.

简单地说,我想在我的仓库中存在一个tmp/log/目录,但是空的.我不希望任何人被骚扰将他们的日志和tmp文件添加到这些目录.

lar*_*sks 6

您是否期望双引号在您的.gitignore文件中表现得像评论 ?你有这个......

*            " Ignore everything in this directory
!.gitignore  " Except this file
Run Code Online (Sandbox Code Playgroud)

......但那就是你想要的.如果你简单地替换它:

*
!.gitignore
Run Code Online (Sandbox Code Playgroud)

它会工作得很好.看:

$ ls -A
dir1 .git
$ ls -A dir1
.gitignore
$ cat dir1/.gitignore
*
!.gitignore
$ git status
# On branch master
nothing to commit (working directory clean)
$ touch dir1/file{1,2,3}
$ ls -A dir1
file1 file2 file3 .gitignore
$ git status
# On branch master
nothing to commit (working directory clean)
Run Code Online (Sandbox Code Playgroud)