Can git filter out specific lines before committing?

I have a github repo that I work from, and I often have comments on my files .py

that start with " # TODO:

" to leave a personal note on what to do.

# TODO: do this <code>

      

I obviously don't want this to happen in the commit.

I want GitHub to go through all the files when I am about to commit them, and not include the lines starting with # TODO:

Does Git already do this? I know that some version controls like perforce already have this feature.

Any thoughts?

+3


source to share


1 answer


I want GitHub to go through all the files when I am about to commit them, and not include the lines starting with # TODO:

GitHub (server side) won't do this.

But you can register a content filter filter in your local repo that will do this for you on ( like a ). git commit

sed '/^# TODO:/ d'

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

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

A ' clean

' filter can filter out these lines (I will not discuss whether to keep them or not) and this filter will be applied automatically on Git commit.
This will obviously delete everything TODO

, including others left by others, so handle this with care: it is technically possible, but you have to determine if it is necessary / useful in your case.




Update Feb 2016: With Git 2.8, even if you have defined a Git content filter, you can add Git pointwise without applying a pure filter:

See commit 1a8630d (29 Jan 2016) by Lars Schneider ( larsxschneider

)
.
(merge Junio ​​C Hamano - gitster

-
in commit a3764e7 , Feb 10, 2016)

convert

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

"

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

      

0


source







All Articles