git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How do I qualify paths in the .gitignore file w.r.t. the repo root directory?

From
Sitaram Chamarty <sitaramc@gmail.com>
Date
Feb 24, 2009, 09:07 UTC
Message-ID
<slrngq7e6c.iti.sitaramc@sitaramc.homelinux.net>
In-Reply-To
<7v1vtomhz1.fsf@gitster.siamese.dyndns.org>
On 2009-02-24, Junio C Hamano <gitster@pobox.com> wrote:
> I think you are looking for "/foo/".  From Documentation/gitignore.txt:

[lots of very clear and detailed explanation snipped for brevity...]

I'd been sort of struggling with the part of 'man gitignore' that describes the rules for the exclusion patterns; it just didn't seem as clear as it could have been. It's very accurate, but I (and I noticed a few others on irc) had to read very carefully to do anything moderately complex.

A few days ago, 'doener' (Björn Steinbrink) came up with some much simpler rules that said the same thing, and -- building on the insight that his rules gave me -- I came up with these:

----->8-----

Note that rule 1 merely *modifies* rules 2 and 3, it does not supercede or preclude them.

1.  If you pattern ends with a slash, it matches only
    directories (and their contents)
2.  If there is no slash otherwise, it matches that name, at
    any depth in the tree
3.  If there is a slash anywhere else, it matches that name,
    relative to the .gitignore (or $GIT_WORK_TREE if the
    pattern is from one of the other pattern sources like
    `.git/info/exclude` etc)

The wildcards (`*` and `?`) do not match slashes, but otherwise the patterns are normal shell globs as defined by fnmatch(3) with the FNM_PATHNAME flag set.

----->8-----

Those rules are meant to clarify the following lines from the gitignore man page:

 - If the pattern ends with a slash, it is removed for the
   purpose of the following description, but it would only find
   a match with a directory.  In other words, `foo/` will match a
   directory `foo` and paths underneath it, but will not match a
   regular file or a symbolic link `foo` (this is consistent
   with the way how pathspec works in general in git).
 - If the pattern does not contain a slash '/', git treats it as
   a shell glob pattern and checks for a match against the
   pathname without leading directories.
 - Otherwise, git treats the pattern as a shell glob suitable
   for consumption by fnmatch(3) with the FNM_PATHNAME flag:
   wildcards in the pattern will not match a / in the pathname.
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 15 in “How do I qualify paths in the .gitignore file w.r.t. the repo root directory?”
  1. Brent GoodrickFeb 24, 2009
  2. Junio C HamanoFeb 24, 2009
  3. Sitaram ChamartyFeb 24, 2009
  4. Junio C HamanoFeb 24, 2009
  5. Sitaram ChamartyFeb 24, 2009
  6. Sitaram ChamartyFeb 25, 2009
  7. Junio C HamanoFeb 25, 2009
  8. Sitaram ChamartyFeb 25, 2009
  9. Junio C HamanoFeb 25, 2009
  10. Björn SteinbrinkFeb 26, 2009
  11. Sitaram ChamartyFeb 26, 2009
  12. Sitaram ChamartyFeb 26, 2009
  13. Junio C HamanoFeb 26, 2009
  14. Brent GoodrickFeb 25, 2009
  15. Sitaram ChamartyFeb 25, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.