Can git filter out certain lines before commit?

孤街浪徒 提交于 2019-12-22 10:28:57

问题


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:


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

(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)


来源:https://stackoverflow.com/questions/30945532/can-git-filter-out-certain-lines-before-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!