简体   繁体   中英

.gitignore fails to ignore all files except folders

Basically, I have the following folder structure:

repo 
     README.md
     testing_folder
           readingme.md
     resources_folder

And in my .gitignore file , I want to ignore all files except folders in current directory and this is what I have :

*
!.gitignore
!*/

When I do git status , this is what I get:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   .gitignore

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
    modified:   README.md

Problem is: it doesn't show that the testing_folder and resources_folder are untracked . And when I try to do git add testing/readingme.md , I got the following error message:

The following paths are ignored by one of your .gitignore files:
testing/readingme.md
Use -f if you really want to add them.
fatal: no files added

I am starting to get really confused now because I have read so many other related posts. They seem to work but mine just doesn't work.

I tried including and excluding the !*/ statement in .gitignore file but still the folders are not shown as untracked .

I've recreated the directory structure you showed in the post, and when my .gitignore contains:

/*
!*/                                                                         
!.gitignore

I did notice that when the first line was * instead of /* I had the same problem as you. Adding the / , I would guess, gives git enough context to do what you want. With the /* my git status returns:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    testing_folder/

This will, essentially, only track files which aren't in the parent directory. The reason why resources_folder isn't showing up is because git doesn't track empty directories. There are some ways to trick git into tracking empty folders , but it's not default behavior.

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