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
Brent Goodrick <bgoodr@gmail.com>
Date
Feb 25, 2009, 01:14 UTC
Message-ID
<e38bce640902241714s720a4e23v49e0a4ab22da9bde@mail.gmail.com>
In-Reply-To
<7v1vtomhz1.fsf@gitster.siamese.dyndns.org>

Thanks James and Junio. I'll try the leading slash. I assumed that the beginning slash means what it means for other tools (meaning a fully-qualified path to some file somewhere on the file system), but apparently such is not the case with git (and I conclude from this that it is actually not possible to store fully qualified paths _unless_ the $GIT_DIR is right under the root filesystem). This isn't a problem for me, and probably not for most folks, but it was confusing to me the way it was written in the man page.

Brent
On Mon, Feb 23, 2009 at 11:06 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 64 quoted lines
>
> Brent Goodrick <bgoodr@gmail.com> writes:
>
> > 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: Junio C HamanoNext: Sitaram Chamarty
Message 14 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.