清除git缓存中的pycache

直接删掉硬盘上的文件

JUNWEI1+lijw@junwei1 MINGW64 /d/pythonProject/django-test (master)
$ git rm -r apis/__pycache__
error: the following files have local modifications:
apis/__pycache__/urls.cpython-37.pyc
apis/__pycache__/views.cpython-37.pyc
(use --cached to keep the file, or -f to force removal)

JUNWEI1+lijw@junwei1 MINGW64 /d/pythonProject/django-test (master)
$ git rm -r -f apis/__pycache__
rm 'apis/__pycache__/__init__.cpython-37.pyc'
rm 'apis/__pycache__/admin.cpython-37.pyc'
rm 'apis/__pycache__/apps.cpython-37.pyc'
rm 'apis/__pycache__/models.cpython-37.pyc'
rm 'apis/__pycache__/urls.cpython-37.pyc'
rm 'apis/__pycache__/views.cpython-37.pyc'

如果我想保留硬盘上的这个文件,而只删除版本管理中的文件,就需要加入--cached参数。

切换分支出现问题

JUNWEI1+lijw@junwei1 MINGW64 /d/pythonProject/django-test (master)
$ git checkout download-api
error: Your local changes to the following files would be overwritten by checkout:
apis/__pycache__/urls.cpython-37.pyc
apis/__pycache__/views.cpython-37.pyc
Please commit your changes or stash them before you switch branches.
Aborting

尽管我已经删除了​​__pycache__​​,硬盘也没有了,但是切换分支的时候依然是会提示本地重写的情况。

提示:需要我在切换分支之前,提交一次更新。

$ git commit -m "delete __pycache__"
[master f82b0ab] delete __pycache__
6 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 apis/__pycache__/__init__.cpython-37.pyc
delete mode 100644 apis/__pycache__/admin.cpython-37.pyc
delete mode 100644 apis/__pycache__/apps.cpython-37.pyc
delete mode 100644 apis/__pycache__/models.cpython-37.pyc
delete mode 100644 apis/__pycache__/urls.cpython-37.pyc
delete mode 100644 apis/__pycache__/views.cpython-37.pyc
JUNWEI1+lijw@junwei1 MINGW64 /d/pythonProject/django-test (master)

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 282 bytes | 282.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Powered By Gitee.com
To gitee.com:kubernete/django-test.git
963e5b2..f82b0ab master -> master

提交了更新之后,再来尝试切换分支,如下:

JUNWEI1+lijw@junwei1 MINGW64 /d/pythonProject/django-test (master)
$ git branch
download-api
* master
upload-api

JUNWEI1+lijw@junwei1 MINGW64 /d/pythonProject/django-test (master)
$ git checkout download-api
Switched to branch 'download-api'
Your branch is up to date with 'origin/download-api'.

成功了,说明当做了任何变更之后,切换分支前需要执行​​commit​​一下变化。

好了,为了避免再次会将​​__pycache__​​上传到仓库,下面设置一下忽略。

设置 ​​.gitignore​​​ 忽略 ​​__pycache__​

$ cat .gitignore
/.idea/
*/__pycache__


git 设置 gitignore 忽略 __pycache___python