3

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
pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
Antonio
  • 731
  • 7
  • 28
  • 1
    you want to just remove the lines or disable a push? wouldn't you want to record your TODOs so that later, you know wtf this is busted (answer: because you never implemented the todo) – court3nay Nov 24 '16 at 21:38

2 Answers2

13

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 lose all your TODO's if you accidentally merge in any changes that touch these files.

Community
  • 1
  • 1
1615903
  • 32,635
  • 12
  • 70
  • 99
  • The TODOS being lost on a merge is something to consider, luckily, my branches I work on are worked on by me and me alone, and are more just notes to myself and not super important at that. Anything important is likely documented elsewhere. – Antonio Nov 25 '16 at 22:56
  • Great! Any idea how to also exclude it from changed files list in `git status`? – Ctrl-C Feb 27 '17 at 17:32
  • 1
    @Ctrl-C I don't think that's possible, see http://stackoverflow.com/a/19809121/1615903 – 1615903 Feb 28 '17 at 06:12
4

No, gitignore is used for ignoring files, not lines.

the best you can do is to use a commit hook (check the pre-commit hook at https://git-scm.com/book/it/v2/Customizing-Git-Git-Hooks) that it could remove such lines.

this could be accomplished using sed/awk and some regular expression for instance

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82