24

I built a web app on a my local win7 machine. I did it with pycharm and used git as version control. I'm a total git novice.

I put the repository on github so that I could stage the webapp to my pythonanywhere server.

On pythonanywhere side, I did some small edits to various files. I wanted to commit these changes back to the repository.

(udemy) 10:44 ~/keystone (master)$ git commit -m "got it running on pythonanywhere staging"
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
        modified:   keystone/settings/base.py
        modified:   keystone/settings/local_postgres.py
        modified:   keystone/settings/staging_straits.py
        deleted:    p0150_1.pdf
Untracked files:
        crapboard/__pycache__/
        crapboard/migrations/__pycache__/
        crapboard/templatetags/__pycache__/
        keystone/__pycache__/
        keystone/settings/__pycache__/
no changes added to commit

There were three modified files and one deletion that I wanted to commit to the repository.

so I did

(udemy) 14:03 ~/keystone (master)$ git add --all
(udemy) 14:03 ~/keystone (master)$ git commit -m "staged to pythonanywhere"
[master ac6bb7e] staged to pythonanywhere
 27 files changed, 23 insertions(+), 115 deletions(-)
 create mode 100644 crapboard/__pycache__/__init__.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/admin.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/apps.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/forms.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/models.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/pdf_views.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/urls.cpython-36.pyc
 create mode 100644 crapboard/__pycache__/views.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/0001_initial.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/0001_squashed_0005_auto_20170921_2154.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/0002_auto_20170909_1137.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/0003_auto_20170912_2029.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/0004_problem_author.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/0005_auto_20170921_2154.cpython-36.pyc
 create mode 100644 crapboard/migrations/__pycache__/__init__.cpython-36.pyc
 create mode 100644 crapboard/templatetags/__pycache__/__init__.cpython-36.pyc
 create mode 100644 crapboard/templatetags/__pycache__/crapboard_filters.cpython-36.pyc
 create mode 100644 keystone/__pycache__/__init__.cpython-36.pyc
 create mode 100644 keystone/__pycache__/urls.cpython-36.pyc
 create mode 100644 keystone/settings/__pycache__/__init__.cpython-36.pyc
 create mode 100644 keystone/settings/__pycache__/base.cpython-36.pyc
 create mode 100644 keystone/settings/__pycache__/settings_secret.cpython-36.pyc
 create mode 100644 keystone/settings/__pycache__/staging_straits.cpython-36.pyc
 rewrite keystone/settings/staging_straits.py (65%)
 delete mode 100644 p0150_1.pdf

Argh. It committed all these __pycache__ directories too.

I'm guessing this happened because I should have made some kind of global/general .gitignore file on my pythonanywhere server?

So questions:

1) how do I get rid of this pycache stuff from my repository permanently 2) how do I prevent my pythonanywhere server from trying to add that stuff to my repository in the future -- I do not have this issue with pycharm/local machine -- it ignores those files.

user3556757
  • 3,469
  • 4
  • 30
  • 70
  • 1
    Some clues: https://stackoverflow.com/questions/1139762/ignore-files-that-have-already-been-committed-to-a-git-repository (You probably already have a .gitignore file somewhere in your dir) – doctorlove Sep 26 '17 at 14:20
  • On part (1): note that you cannot *change* any existing commits, but you can stop using them, and eventually, if there's no way to find them, they expire. If you have not *sent* this new commit anywhere else (so that only you have it), you can stop using this commit (by using `git commit --amend` to shove it aside, or `git reset` to hide it away so that even you can't see it). Then you won't have `__pycache__` in any of your (visible) commits, so that you won't send commits that have it on to any other repository either. – torek Sep 26 '17 at 14:49
  • Put it in .gitignore – Jon Deaton Sep 28 '17 at 16:11
  • After adjust gitignore, I had to do this for every file, so I call awk for help: "git status | grep pycache | awk {'print $3'} | xargs git reset HEAD" – André Duarte Oct 04 '19 at 14:22

2 Answers2

42

According to git docs, it is simplely to add to ~/.gitignore the following:

**/__pycache__

A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".

Nguyen Van Duc
  • 1,019
  • 10
  • 9
4
  1. You could do a git rm to delete the folder, then commit and push that. Or if you are feeling super adventurous and nobody else uses your repo, you could do a commit --fixup and then a rebase --interactive to clean everything up. See here for details.
  2. Add __pycache__ to ~/.gitignore. Or better yet just checkin a .gitignore file for your repo.
Kermit
  • 4,922
  • 4
  • 42
  • 74
conrad
  • 1,783
  • 14
  • 28