.gitignore Syntax: bin vs bin/ vs. bin/* vs. bin/**

bin matches any files or directories named ‘bin’.

bin/ matches any directories named ‘bin’, which in effect means all of its contents since Git doesn’t track directories alone.

bin/* matches all files and directories directly in any bin/. This prevents Git automatically finding any files in its subdirectories, but if, say a bin/foo subdirectory is created, this rule will not match foo‘s contents.

bin/** matches all files and directories in any bin/ directory and all of its subdirectories.

The word “any” is critical here since rules are not relative to the repository root and apply anywhere in the filesystem tree. You must begin rules with a / (or !/ to un-ignore) which means the repository’s root, not the system’s root, in order to match only what was intended.

WARNING: You should never use rules like dir/*, /dir/**, etc. alone unless you also un-ignore something that exists inside that directory. Omit the asterisk or you could permanently lose a lot of data from certain invocations of git gc, git stash and more.

I don’t really know what tmp/**/* is meant to do. I initially thought it could be used to match files in the sub-directories of tmp/ but not files directly present in tmp/ itself. But a simple test seems to suggest that this ignores all files in tmp/.

Leave a Comment