简体   繁体   中英

gitignore not working - want to ignore everything but gitignore file to keep an 'empty' directory

I have a .gitignore file in a few directories (ex. log/ ) with the following contents:

*            " Ignore everything in this directory
!.gitignore  " Except this file

Nonetheless, if there is a file in that directory, git status shows it to me saying it's new but untracked.

# 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)

I have found other posts on stackoverflow suggesting putting a comment on the first line and various other forms or random voodoo, but nothing has worked.

Simply, I want a tmp/ and log/ directory to exist in my repo, but empty. I don't want anyone to be harassed to add their log and tmp files to these directories.

Are you expecting the double-quote to act like a comment in your .gitignore file? You've got this...

*            " Ignore everything in this directory
!.gitignore  " Except this file

...but that's doing what you want. If you replace it with simply:

*
!.gitignore

It will work just fine. Watch:

$ 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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