Can git filter out certain lines before commit?

1.5k views Asked by At

I have a repo on github that I am working out of and I often have comments on my .py files that starts with the "# TODO:" to keep a personal note of things to be done.

# TODO: do this <code>

I obviously do not want that to go in a commit.

I want GitHub to search all the files when I am about to commit them and not include lines that start with # TODO:

Does Git already do this? I know certain version control like perforce already have this feature.

Any thoughts?

1

There are 1 answers

2
VonC On

I want GitHub to search all the files when I am about to commit them and not include lines that start with # TODO:

GitHub (server side) won't do that.

But you can, in your local repo, register a content filter driver which will do that for you on git commit (like a sed '/^# TODO:/ d').

https://i.stack.imgur.com/EQiwd.png

(image shown in "Customizing Git - Git Attributes", from "Pro Git book")

A 'clean' filter can filter out those lines (I won't discuss if you should leave them or not), and that filter will apply automatically on git commit.
That would obviously remove all TODO including ones left by others, so handle this with care: it is technically possible, but you have to determine if it is needed/useful in your case.


Update February 2016: with git 2.8, even if you had defined a git content filter, you can git add punctually without applying the clean filter if needed:

See commit 1a8630d (29 Jan 2016) by Lars Schneider (larsxschneider).
(Merged by Junio C Hamano -- gitster -- in commit a3764e7, 10 Feb 2016)

convert: treat an empty string for clean/smudge filters as "cat"

git -c filter.myFilter.clean= add test.disable
                            ^^
                          (empty value)