743

Can you write comments in a .gitignore file?

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

2
  • 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, 2012 at 0:03
  • 18
    So you are looking for a .gitignoreignore?
    – daviewales
    Feb 26, 2015 at 6:37

2 Answers 2

904

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
5
  • 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. Aug 29, 2012 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, 2012 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. Sep 9, 2012 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, 2017 at 1:38
  • Will there be multiline comments introduced?
    – Timo
    Sep 22, 2020 at 6:26
269

Do git help gitignore.

You will get the help page with following line:

A line starting with # serves as a comment.

5
  • 82
    +1 for pointing out how to find information, not just giving information. Jul 22, 2014 at 8:47
  • 12
    @cregox - Also, I like using computers without command lines - git is wrong place for you then, sorry.
    – manojlds
    Feb 24, 2016 at 9:37
  • 4
    @manojlds git is right place for everyone, just like rsync. they just need good gui's, like dropbox. too sad we're still missing one for git...
    – cregox
    Feb 24, 2016 at 10:49
  • 2
    You could also do git help ignore (which is supported by git bash-completion)
    – user202729
    Oct 21, 2019 at 16:43
  • This doesn't tell you how to find information. Finding information starts with git help help.
    – kon psych
    Mar 21, 2020 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.