This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 5: Objectify your robot!

git ignore

face Josiah Wang

Now, if you look at your folder, you may notice that there might be a new subfolder called __pycache__, which contains a file robot.cpython-38.pyc (this might vary a bit depending on your system).

If you remember back way back in Lesson 1, I mentioned that Python will compile your script and generate an intermediate representation called byte code, and then execute your program by interpreting this byte code. Ring any bells? If not, revisit Lesson 1 Chapter 2.5!

The official Python interpreter will generally pre-compile any modules that you write into byte code. The interpreter can then use this pre-compiled byte code directly, rather than having to recompile it each time your program runs.

That’s fine and all. But such byte codes are not really worth committing to your repository, are they? They can automatically be regenerated by Python. What’s important and needs to be committed are your scripts themselves!

If you run git status, you will see __pycache__/ listed in the untracked files list. We might actually accidentally add this directory to the repo, for example with git add . which adds all files to the repo.

user@MACHINE:~/robot$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   main.py

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        __pycache__/
        robot.py

no changes added to commit (use "git add" and/or "git commit -a")

How can we get Git to ignore this directory so that this file does not accidentally get added to the repo?

This is pretty easy. You just create a .gitignore file to tell Git to always ignore this folder/file! We will do this on the next page!