1.11. Gitignore files

The process of doing homeworks or working on projects will inevitably create files that you might not want to sync to the remote repo on GitHub.com. Because the remote repo is “public” facing, the goal is to have a clean remote repo without miscellaneous files.

Common versions of files you might not want on the remote repo:

  • .DS_Store (a temporary file generated by macOS)

  • ipynb checkpoint files

  • temporary data files (such as mid point saves, data files you examined to check)

So how do we prevent git from syncing these files? The .gitignore file!

1.11.1. How to create and edit a gitignore file

There are several ways to create one:

  1. When you create a repo, select to add a gitignore file. I usually pick the python template option.

  2. You can add it later on github or on your computer. The file is a simple text file and should be named .gitignore

  3. In GitHub desktop, choose the “Repository” menu, then “Repository settings”, then “ignored files” and type in what you’ll ignore.

Editting it: Any text editor!

1.11.2. Syntax

Each line lists a file(s) or folder(s) that shouldn’t be tracked. Wildcards are allowed. For example,

.ipynb_checkpoints
*/.ipynb_checkpoints/*

should prevent the checkpoints Jupyter Lab makes from being synced.

1.11.3. I have a file in gitignore, but it’s still on the remote repo!

That’s because it was already on the remote repo when you added it to the gitignore; The gitignore file simply tells git to “not sync” those files any more. It doesn’t mean “delete these files from the remote repo”.

There are a few ways to delete the extra files in the remote repo. The simplest: Go to the repo on Github.com and delete them individually.

If you have lots of files you want to suddenly gitignore/delete and you are comfortable running some git commands, you can do it programmatically. Open terminal/powershell, move to the current directory to the root folder of your repo, and then run these commands in order:

git rm -r --cached . 
git add .
git commit -m 'Removed all files that are in the .gitignore' 
git push origin master

1.11.4. More resources

The manual for git describes how to use these files in much more detail.