Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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
like image 816
Antonio Avatar asked Apr 07 '26 13:04

Antonio


2 Answers

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.

like image 113
1615903 Avatar answered Apr 09 '26 01:04

1615903


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

like image 34
pedrorijo91 Avatar answered Apr 09 '26 01:04

pedrorijo91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!