738

Can you write comments in a .gitignore file?

If so, should the line be preceded with a # or some other indicator?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
James Raitsev
  • 92,517
  • 154
  • 335
  • 470
  • 4
    If you use an editor that knows about these things (e.g. Vim), the lines commented with `#` will be formatted appropriately - easy to discover yourself that way! – Cascabel Jan 15 '12 at 00:03
  • 18
    So you are looking for a .gitignoreignore? – daviewales Feb 26 '15 at 06:37

2 Answers2

899

Yes, you may put comments in there. They however must start at the beginning of a line.

cf. http://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository#Ignoring-Files

The rules for the patterns you can put in the .gitignore file are as follows:
- Blank lines or lines starting with # are ignored.
[…]

The comment character is #, example:

# no .a files
*.a
Nicolas Raoul
  • 58,567
  • 58
  • 222
  • 373
TimWolla
  • 31,849
  • 8
  • 63
  • 96
  • 4
    Don't know if it was true when this was answered, but the linked book section indicates that in addition to starting a line, comments can also be _appended_ to a line in the .gitignore file. – Stuart R. Jefferys Aug 29 '12 at 22:03
  • 9
    @StuartR.Jefferys I'm on git 1.7.4.1 and am finding that a line with a comment at the end does not work: `src/main/log/ # Doesn't work. git status still shows this directory` `src/main/log/` Works fine. git status does not show the directory. In fact, it appears that _any_ whitespace at the end of the line is considered part of the ignore pattern. – Johann Aug 30 '12 at 19:23
  • 9
    @Johann You are right! Trailing whitespace _is_ significant, even with directories (ending in `/`). If a file has a trailing space, the `.gitignore` entry must match; 0 or 2 spaces and it fails. I consider it a bug. I'm using git version 1.7.5.4. It _could_ be intentional, even if it probably _shouldn't_. But you can use `[ ]` as a space character specifier. That is much better than allowing trailing white-space; it allows for the rare intentional trailing space, while making the more common (and hard to see) error case detectable. – Stuart R. Jefferys Sep 09 '12 at 20:57
  • 2
    Thanks for this; I was having the hardest time trying to understand why `git` was ignoring my `.gitignore`. It was because I put **end-of-line comments** after some entries. The default VIM syntax coloring for `config` filetypes misled me. – Luke Davis Dec 15 '17 at 01:38
  • Will there be multiline comments introduced? – Timo Sep 22 '20 at 06:26
267

Do git help gitignore.

You will get the help page with following line:

A line starting with # serves as a comment.

nishanthshanmugham
  • 2,967
  • 1
  • 25
  • 29
manojlds
  • 290,304
  • 63
  • 469
  • 417