You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm中如何使文件仅在本地Git保留而不上传至GitHub?

Absolutely, this is a super common need—let’s break down exactly how to handle it depending on your current situation:

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 (or Cmd+Shift+N on Mac), and type .git/info/exclude to 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 .gitignore file. 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:

  1. Open PyCharm’s Terminal tab at the bottom of the window.
  2. 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/
    
  3. Commit this change to your local repo:
    git commit -m "Remove local-only files from remote repository"
    
  4. Push the commit to GitHub to update the remote repo:
    git push origin main
    
  5. 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 .gitignore if you want all collaborators to ignore certain files (like virtual environments, IDE configs). You’ll commit .gitignore to GitHub so everyone gets the rules.
  • Use .git/info/exclude only 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:49:49