0

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.

mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96

1 Answers1

2

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.

Community
  • 1
  • 1
Eric Palace
  • 322
  • 2
  • 14
  • I am getting a little bit confused now. So what is the major difference between `/*` and `*` ? And when to use which ? – mynameisJEFF Dec 03 '14 at 23:04
  • To be completely honest, I'm not really sure why `/*` works when `*` doesn't. I think generally if you use `*`, git will be VERY liberal in ignoring things, as you're telling it to just go ahead and ignore everything. Using `/*`, while seeming pretty similar, is explicitly not ignoring the root and that might let you be a bit more strict with what gets ignored and what is included. – Eric Palace Dec 04 '14 at 14:37