简体   繁体   English

提交到 Github 时如何忽略文件的某些部分?

[英]How to ignore parts of a file when committing to Github?

I am working on a project in Python, and some of my test cases include personal information.我正在用 Python 做一个项目,我的一些测试用例包含个人信息。 Currently, my testing workflow involves me just running each module as __main__ and running through the specific test cases for which I am interested in seeing output.目前,我的测试工作流程只涉及将每个模块作为__main__运行并运行我有兴趣查看输出的特定测试用例。 I expect this problem to also present when I move to a more traditional unit test methodology with a tests directory, etc.我希望当我转向带有测试目录等的更传统的单元测试方法时,也会出现这个问题。

Is there a way to ignore parts of a file when I add it to my Git commit?当我将文件添加到我的 Git 提交时,有没有办法忽略文件​​的一部分? Basically, I've got ~200 lines of code, and then test lines:基本上,我有大约 200 行代码,然后是测试行:

if __name__ == '__main__':
    my_function(personal information)

I'd like to commit everything above the test lines, and leave the test lines out of my repository, where it could be available to others.我想提交测试行上方的所有内容,并将测试行保留在我的存储库之外,以便其他人可以使用。

Again, I imagine this problem will present itself down the road as well, when I hard-code the personal information into my unit tests.同样,当我将个人信息硬编码到我的单元测试中时,我想这个问题也会出现。

git add -p allows you to interactively select each chunk that you want to be added to staging. git add -p允许您以交互方式选择要添加到暂存的每个块。


Aside: This -p / --patch flag is also present in other daily-use commands like checkout when trying to “reset” a particular file (equivalent of the new restore command). --patch :这个-p / --patch标志也出现在其他日常使用的命令中,例如在尝试“重置”特定文件(相当于新的restore命令)时的checkout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM