Antonio Antonio - 7 days ago 5
Git Question

How do I tell .gitignore (or git itself) to ignore lines containing # TODO

I have a lot of lines with

# TODO
for future references. These are personal, and I'd like to not commit them along with the rest of my file when I make changes.

Is there any way to tell .gitignore (or git itself) to not include any line containing
# TODO


Or as a bonus.. Not just the line, but maybe from the
# TODO
to the end of line incase I have a
TODO
on the same line there is code

EXAMPLE:

def destroy
# TODO Alter route (Tell git to ignore this)
@comment.destroy
end


OR:

def destroy
@comment.destroy # TODO Alter this line. (Tell git to ignore from '# TODO' onwards.
end

Answer

You could use a 'clean' filter. It is run before a file is staged.

First, define the file types that you want to use the filter for, and put them in your .gitattributes file:

.rb filter=removetodo

Then, adjust your config so that you tell git what the "removetodo" filter is supposed to do on clean:

git config filter.removetodo.clean "sed '/TODO/d'"

sed command taken from Delete lines in a text file that containing a specific string

Then, when you do a git add, git will silently remove every line containing the word TODO from your .rb files, without affecting your working area.

The downside is that you will loose all your TODO's if you accidentally merge in any changes that touch these files.

Comments