Appendix B — Gitignore & Git Untracking

This is a review on how to never track and untrack files in Git using the terminal and the .gitignore file.

B.1 Gitignore

The easiest way to have Git never track a file or directory within your Git directory is to add it to the .gitignore file before you ever start tracking it.

From the Git documenatation:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.

This means the files listed in the .gitignore will be ignored when making a commit. Git does not track changes on it and they do not appear in the index when we check the Git status.

B.2 Creating a .gitignore

B.2.1 When creating a GitHub repository

The easiest way to create a .gitignore is probably when creating a repository through GitHub. To do this:

  1. Go to the “Add .gitignore” section and…

  1. …select a template from the list. These templates are provided by GitHub to ignore unuseful files in different programming languages. For this course we select the Python .gitignore template.

  1. When you finish creating your new repository, the .gitignore file will be in it.

B.2.2 From the GitHub .gitignore templates

If you don’t have a .gitignore yet and want to add the one from the GitHub template you can:

  1. Download the .gitignore GitHub template: https://github.com/github/gitignore.

  2. Move it to your directory.

  3. Update the name to .gitignore.

B.2.3 From the command line

What if we we just want to create our own blank .gitignore? No problem, we can create one from our terminal:

  1. Open the terminal.

  2. Verify you don’t have a .gitignore by running:

ls -a
ls -a

Adding -a to the ls (list) command will show all the files, including the hidden files that start with a period ., such as the .gitignore.

  1. Create a new .gitignore file for the directory:
touch .gitignore

If there is no output, everything worked.

  1. Check your .gitgnore is there:
ls -a

You should see the .gitignore now listed in the files.

B.3 Editing .gitignore

B.3.1 Add a file

Suppose you have an untracked file called example.txt that you want to add to the .gitignore. Remember, untracked means Git hasn’t began tracking changes on this file. If you run git status, example.txt would appear in the index under Untracked files.

If you are using JupyterLab, you can edit the .gitignore from the terminal. Follow these steps to add example.txt to it:

  1. Open the terminal.

  2. Open the .gitignore file in the nano command line-based text editor by running:

nano .gitignore
  1. Once in the editor, add a new line with the name of the file: example.txt.

  2. To exit nano:

    1. Ctrl+X,
    2. Press Y to save the changes
    3. Press Enter
  3. When you run git status again, example.txt will not be listed under the untracked files.

B.3.2 Add a directory

Suppose you want to add all the contents of a directory named data to your gitignore. If none of the files in the data directory have been tracked by Git, then:

  1. Open the terminal.

  2. Open the .gitignore file in the nano command line-based text editor by running:

nano .gitignore
  1. Once in the editor, add a new line with the name of the directory: data/.

  2. To exit nano:

    1. Ctrl+X,
    2. Press Y to save the changes
    3. Press Enter
  3. When you run git status again, data/ will not be listed in the untracked files.

B.4 Untracking

Once a file has been added to a commit, Git starts tracking it. If we add the file’s name to the .gitignore after it has been commited, Git will keep tracking changes in it. To stop tracking a tracked file example.txt:

  1. Open the terminal.

  2. Run git rm --cached example.txt. The git rm --cached command will remove the file from the Git tracking while leaving the file untouched on disk. If you run git status, it will now appear in the index as deleted. That’s ok! Your file is still there, it’s just “deleted” from Git.

  3. If it’s not already there, add example.txt to .gitignore so Git ignores it.

B.5 References

GitHub Blog - How to undo (almost) anything with Git