PyCharm中如何使文件仅在本地Git保留而不上传至GitHub?
Absolutely, this is a super common need—let’s break down exactly how to handle it depending on your current situation:
Scenario 1: Haven’t shared the project on GitHub yet (recommended approach)
If you haven’t pushed anything to GitHub yet, you can set up local-only ignores so your files stay in your local Git history but never get sent to the remote repo:
- Open PyCharm, press
Ctrl+Shift+N(orCmd+Shift+Non Mac), and type.git/info/excludeto open this hidden local ignore file. This file lives only in your local Git setup and won’t be pushed to GitHub. - Add paths for the files/folders you want to keep local, using the same syntax as a regular
.gitignorefile. For example:# Local Django config (not for shared repo) local_settings.py # Personal test data folder local_test_data/ # Local log files logs/local/ - Save the file. PyCharm will immediately respect these rules: you can still commit these files to your local Git repo, but they’ll be automatically excluded when you use the "Share Project on GitHub" feature (or any future pushes).
Scenario 2: Already shared the project on GitHub
If you accidentally pushed the local-only files to GitHub, you can remove them from the remote repo while keeping your local copies intact:
- Open PyCharm’s Terminal tab at the bottom of the window.
- Run this command to remove the files from Git’s tracked list (but keep them on your local machine):
# For a single file git rm --cached path/to/your/local_file.py # For a folder (add -r flag) git rm --cached -r path/to/your/local_folder/ - Commit this change to your local repo:
git commit -m "Remove local-only files from remote repository" - Push the commit to GitHub to update the remote repo:
git push origin main - Finally, add these file/folder paths to
.git/info/exclude(following Scenario 1’s steps) to make sure they never get pushed again.
Quick note: .gitignore vs .git/info/exclude
Just to clarify the difference so you pick the right tool:
- Use a project-level
.gitignoreif you want all collaborators to ignore certain files (like virtual environments, IDE configs). You’ll commit.gitignoreto GitHub so everyone gets the rules. - Use
.git/info/excludeonly for files that you alone need to keep local (personal configs, test data, etc.). This file stays on your machine and never leaves your local Git setup.
内容的提问来源于stack exchange,提问作者IordanouGiannis
相关产品推荐
相关产品推荐

