简体   繁体   中英

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. 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. 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? Basically, I've got ~200 lines of code, and then test lines:

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.


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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