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
Junio C Hamano <gitster@pobox.com>
Date
Feb 24, 2009, 07:06 UTC
Message-ID
<7v1vtomhz1.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<e38bce640902232247t63a37f63x9f403fbda0744cfd@mail.gmail.com>
Brent Goodrick <bgoodr@gmail.com> writes:
Show 24 quoted lines
> Say I have these files and directories [2]:
>
>   /home/smart_tator/misc_files/.gitignore
>   /home/smart_tator/misc_files/foo/
>   /home/smart_tator/misc_files/bar/
>   /home/smart_tator/misc_files/bar/baz/foo/
>   /home/smart_tator/misc_files/bar/baz/real/
>
> then I do:
>
>   cd /home/smart_tator/misc_files/; git init
>
> and say I have this line in that .gitignore file:
>
>   foo/
>
> And then I naively execute:
>
>   git add bar/
>
> then the bar/baz/real/ is added, but these are dutifully ignored:
>
>   /home/smart_tator/misc_files/foo/
>   /home/smart_tator/misc_files/bar/baz/foo/
I think you are looking for "/foo/".  From Documentation/gitignore.txt:
 - 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).

With this rule, (1) the trailing slash in your "foo/" tells git to match only with directories, but (2) it behaves as if you said "foo" for all the other rules.

With "/foo/", you tell git to match only with a directory, and it is as if you said "/foo".

 - 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.

Your "foo/" now behaves the same way as "foo" behaves. You are telling git to match directory foo anywhere in the tree. "/foo/" (now behaving the same way as "/foo") does not satisfy this criteria so we would skip this rule.

 - 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.
   For example, "Documentation/\*.html" matches
   "Documentation/git.html" but not
   "Documentation/ppc/ppc.html".  A leading slash matches the
   beginning of the pathname; for example, "/*.c" matches
   "cat-file.c" but not "mozilla-sha1/sha1.c".

Your "foo/" does not survive to this rule, but "/foo/" does. It now behaves as "/foo" and its leading slash makes it match the beginning.

Previous: Brent GoodrickNext: Sitaram Chamarty
Message 2 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.