4

I have folders like so:

/foo/one.txt
/foo/bar/two.txt
/foo/other/three.txt

I want to exclude everything in the folder /foo/ except the subfolder /foo/bar/. How do I do that?

With this .gitignore I've managed to exclude the "other" subfolder, but the file "one.txt" is still included.

/foo/*
!/foo/bar/
exstral
  • 639
  • 1
  • 5
  • 8

2 Answers2

0

Like Charles Bailey said in the comments section, your code works:

$ cat .gitignore 
/foo/*
!/foo/bar/

$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   .gitignore
#   foo/
nothing added to commit but untracked files present (use "git add" to track)
$ git add foo/ -n
add 'foo/bar/two.txt'
$ 
Umang
  • 5,196
  • 2
  • 25
  • 24
0

It may be that while the .gitignore is now working you are suffering from the common problem of needing to remove a tracked file from the index that is now in gitignore e.g. why-git-doesnt-ignore-my-specified-file

That is, use git rm <file> to remove / make sure it is not in the index.

Community
  • 1
  • 1
Philip Oakley
  • 13,333
  • 9
  • 48
  • 71