Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

github - Can git filter out certain lines before commit?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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').

http://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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...