18

I have many subfolders in my repo, in which there are few build/ directories I'd like to ignore when committing my code. As I understand it, there are two ways to ignore all of the folders:

  • */build/ (Unix wildcards)
  • build/ (git way of ignoring files/folders)

I've found Git ignore sub folders but it has two answers and I would like to know what the difference (none?) between the two approaches is. Does the same rule apply to files?

Community
  • 1
  • 1
syntagma
  • 23,346
  • 16
  • 78
  • 134

1 Answers1

38

build/ is the right way to do it. It will ignore any files that are inside build directories no matter how deep are these directories nested in your repo.

*/build/ means ignore build directories that are exactly one level deep inside your repo. So for instance files inside build and foo/bar/build won't be ignored.

If you need more fine grained control, you can always add specific directories to exclude but also not to exclude using ! as a prefix.

Simon
  • 31,675
  • 9
  • 80
  • 92
  • 2
    I've tried all sorts of variations on this and can't get `git add .` on a bare repo to ignore a directory and all files in all subdirectories of that directory. I have tried `dirname/`, `dirname/*`, `dirname/**`, `dirname/*/*`, even in desperation `dirname/*/*/*`, `dirname/*/*/*/*`, `dirname/*/*/*/*/*`. – Chris Nov 20 '14 at 17:21
  • Thank you @Simon, so it must be something else. I don't mean --bare. I run `git init` then `git add --all .` (with or without --all) in my directory full of source files and `.gitignore` and it includes every non-empty subdirectory under my named directory. I'm using 1.9.4.msysgit.1 Solved it for now by removing the folders then re-adding them after indexing, thought I'd misunderstood the docs or syntax. – Chris Nov 21 '14 at 14:44
  • Guess this'll need some more digging to provide enough info to diagnose, but thanks for reassuring me I'm not on the wrong track. – Chris Nov 21 '14 at 14:52
  • 1
    Seems you actually have to remove the directory and re-add before it takes effect. That's what happened to me too. – Alma Jul 19 '16 at 18:20