Ignoring Files in Git

Let’s say I want­ed to add a devel­op­ment notes files in my project to keep track of my per­son­al project-relat­ed notes. I want this in the project direc­to­ry so it’s eas­i­ly acces­si­ble. How­ev­er, I don’t want to track it in the repos­i­to­ry because oth­er peo­ple on my team also keep their own notes and we don’t want to over­ride each other’s files.

To get around this, I can put the file in the repos­i­to­ry direc­to­ry but tell Git to ignore it and any changes to it.

We ignore files in Git using the .gitignore file locat­ed in the root of the project.

I cre­at­ed the .gitignore file for the same repos­i­to­ry. We can see it when list­ing the files with hid­den files showing.

$ ls -al
  total 72
  drwxr-xr-x@ 11 ryan  staff   374 Apr  1 12:01 .
  drwxr-xr-x   4 ryan  staff   136 Apr  1 11:01 ..
  -rw-r--r--@  1 ryan  staff  6148 Apr  1 11:01 .DS_Store
  drwxr-xr-x  13 ryan  staff   442 Apr  1 12:58 .git
  -rwxr-xr-x@  1 ryan  staff     9 Dec 15  2009 .gitignore
  -rwxr-xr-x@  1 ryan  staff  2236 Apr  1 12:03 1-basics.rb
  -rwxr-xr-x@  1 ryan  staff  1110 Dec 15  2009 2-expressions_and_operators.rb
  -rwxr-xr-x@  1 ryan  staff   764 Dec 15  2009 3-objects_and_classes.rb
  -rwxr-xr-x@  1 ryan  staff  1317 Dec 15  2009 4-inheritance.rb
  -rwxr-xr-x@  1 ryan  staff  1183 Dec 15  2009 5-modules_and_mixins.rb
  -rwxr-xr-x@  1 ryan  staff   262 Dec 15  2009 README.md

Let’s open it up and take a look.

$ vim .gitignore

It was one line right now to ignore the .DS_Store file that macOS generates.

I’m also going to have it ignore my dev-notes.txt file that I want to create.

First, let’s cre­ate the file:

  touch dev-notes.txt

Next we’ll use git-status to see that Git picks it up as an untracked file. We don’t want to track this file with Git, so we can leave it as-is.

$ git status
  On branch master
  Untracked files:
    (use "git add <file>..." to include in what will be committed)
	
    dev-notes.txt
	
  nothing added to commit but untracked files present (use "git add" to track)

Now in the .gitignore file we need to add an entry for the the notes text file. Open it up, and under the line for .DS_Store add this:

dev-notes.txt

Save the file and then run:

$ git status
  On branch master
  Changes not staged for commit:
    (use "git add <file>..." to update what will be committed)
    (use "git checkout -- <file>..." to discard changes in working directory)
	
    modified:   .gitignore
	
    no changes added to commit (use "git add" and/or "git commit -a")

The untracked dev-notes.txt file is gone (because Git is ignor­ing it now) but the .gitignore file is show­ing up as modified.

Let’s add and com­mit the changed .gitignore file to our repos­i­to­ry. If we’re shar­ing this repos­i­to­ry with oth­er devel­op­ers, they’ll get the ben­e­fit of also hav­ing their notes file (assum­ing they fol­lowed the nam­ing con­ven­tion) also being ignore by Git.

$ git commit -a -m "added dev notes file to ignore list"
  [master 4a994e2] added dev notes file to ignore list
  1 file changed, 2 insertions(+), 1 deletion(-)

Let’s add a note to our dev-notes.txt file just to check that our changes won’t show in Git.

I added a bul­let at the top:

* switch `cities` array when teaching in a different state

And now if we run git-status, we should still see a clean work­ing directory.

$ git status
  On branch master
  nothing to commit, working directory clean