Ignoring Files in Git
Let’s say I wanted to add a development notes files in my project to keep track of my personal project-related notes. I want this in the project directory so it’s easily accessible. However, I don’t want to track it in the repository because other people on my team also keep their own notes and we don’t want to override each other’s files.
To get around this, I can put the file in the repository directory but tell Git to ignore it and any changes to it.
We ignore files in Git using the
.gitignore file located in the root of the project.
I created the
.gitignore file for the same repository. We can see it when listing the files with hidden files showing.
$ ls -al total 72 [email protected] 11 ryan staff 374 Apr 1 12:01 . drwxr-xr-x 4 ryan staff 136 Apr 1 11:01 .. [email protected] 1 ryan staff 6148 Apr 1 11:01 .DS_Store drwxr-xr-x 13 ryan staff 442 Apr 1 12:58 .git [email protected] 1 ryan staff 9 Dec 15 2009 .gitignore [email protected] 1 ryan staff 2236 Apr 1 12:03 1-basics.rb -rwxr[email protected] 1 ryan staff 1110 Dec 15 2009 2-expressions_and_operators.rb [email protected] 1 ryan staff 764 Dec 15 2009 3-objects_and_classes.rb [email protected] 1 ryan staff 1317 Dec 15 2009 4-inheritance.rb [email protected] 1 ryan staff 1183 Dec 15 2009 5-modules_and_mixins.rb [email protected] 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 create the file:
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:
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")
dev-notes.txt file is gone (because Git is ignoring it now) but the
.gitignore file is showing up as modified.
Let’s add and commit the changed
.gitignore file to our repository. If we’re sharing this repository with other developers, they’ll get the benefit of also having their notes file (assuming they followed the naming convention) 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 bullet 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 working directory.
$ git status On branch master nothing to commit, working directory clean