views:

257

answers:

3

Git treats lines starting with # as comment lines when committing. this is very annoying when working with a ticket tracking system, and trying to write the ticket number at the beginning of the line, e.g.

#123 salt hashed passwords

git will simply remove the line from the commit message. is there any way to escape the hash? i tried \ and !, but nothing works. whitespaces before # are preserved, so they aren't a working solution to the problem either.

+2  A: 

Use a different prefix for the ticket number. Or prepend a word to the ticket number, like "Bug #42". Or prepend a single space character to the line; if you wish to remove that whitespace you can add a commit-hook for that.

I personally would rather not have this sort of commit message manipulation done by a hook because it can be very irritating when it triggers when you don't want it to. The easiest solution is probably to re-think the problem.

wilhelmtell
different wording is only a workaround for the problem. if project guidelines state that a commit message must begin with the ticket id, then it will not work. and a post-commit hook is very ugly. i think i should report this "bug" to the git developers
knittl
Don't bother, that'd be wrong. Don't ask the git developers to work according to your guidelines. You wouldn't ask Dennis Ritchie to change the C language so it supports you variable names convention of starting with a hash character, right? The same applies here. If commit messages allow comments then this adds support for interesting things, like opening the commit editor with the diff added and commented out so you don't need to remember your exact changes. What's wrong with preserving the leading space character?
wilhelmtell
supporting escape characters in git's commit message wouldn't be such a big deal
knittl
+3  A: 

You can use the command line option -m:

git commit -m "#123 fixed"
Olivier
ok, this is an easy workaround for now. thanks
knittl
+6  A: 

This behaviour is part of git commit's default 'clean-up' behaviour. If you want to keep lines starting with # you can use an alternative clean-up mode.

E.g.

git commit --cleanup=whitespace

If you do this you have to be careful to remove all # lines that you don't want to appear in the commit.

Charles Bailey
thanks, this works best so far and comes close to escaping :)
knittl