Ignoring Files in Git

Sometimes you want to not include files in a repository, files like help files, object files, and temporary files, for example. It's good to ignore non-source files, because it reduces the number of files you track, and even though Git is the fastest version control software, it still speeds up how fast Git will run. The less files the faster Git is, although I have to admit, it's not much of an issue with Git.

As an introduction to ignoring these files let's first define how Git classifies files.

Git groups your repository files into three categories:

Tracked files. Tracked files are any files in your repository, or any files you have staged, and are getting ready to be added to your repository. Tracked files are part of your current "index" of files.

Untracked Files. Untracked files are new files you may have added, since the last commit, and files you have changed, but have not yet staged to be committed, that is files you haven't run "git add" on yet. Untracked files show up when you run "git status."

Ignored files. Ignored files are files, or directories, that you designate to be ignored in the repository. Files that will be invisible to Git, even if they are modified in some way.

In order to ignore files in Git, you have to explicitly tell Git what files, or directories, you would like to ignore. This is done in one of two files.

The ".git/info/exclude" file ignores files located in any directories and sub directories in your current Git repository. The exclude file is created when you init your repository, and is located in your hidden Git directory under "info." Any files listed in the "exclude" file will be ignored by Git.

The ".gitignore" file, which is not created, when you first set up your repository by running "git init." You will have to explicitly create a .gitignore file where you want it. A .gitignore file can be placed in any sub directories under the top directory. Files declared in ".gitignore" will be ignored in the current directory and any sub directories below. Most folks just place the .gitignore file in the top directory and they're set.

What is the difference between ".git/info/exclude" and ".gitignore" ? The exclude file is not passed on to other users with a clone command while the ".gitignore" file actually, becomes a tracked file in your repository, and travels with the repository every time the repository is cloned.  This means that a group of developers could have a ghost file that changed, but is ignored floating around, but in reality the fact that a file does not show up in a git status that a developer just modified is a clue for the developer to change the file name, or check the .gitignore file.

Both of these files are text files that are edited with a Linux text editor, like vi. The rules and syntax for ignoring files are:

  • One file or directory per line.
  • Blank lines are ignored.
  • Lines with a # are used for comments.
  • A simple file name will match any file with that name in every directory applicable to .gitignore.
  • Directory names are indicated with a trailing / which includes any sub directories.
  • You can use * as a wild card, but can only match files in the designated directory, like so: "dev/test/*.o" will match all files in test/ ending with ".o".
  • You can have "not" files that are never ignored by starting the listing with a "!".

The contents of a two .gitignore files with ignored files, and a .git/info/exclude file with no files added.

There is one gotcha. If you commit files and then add them to the .gitignore file, "git status" will show these ignored files in your untracked files ready for a commit.

In order to get around this, you have to remove the files from the git index with "git rm -cached " This will take it out of the "git status" command. Warning if you just run "git rm ," it will delete the file, which may not be what you want to do. The "cached" is important.

There is an alternative way to do this, if the rm command makes you uncomfortable, that is, using the "git-update-index --remove " command to modify the current working index and directory cache. Both of these methods will clear previously committed, but now ignored files, out of your git status, and have them ignored in your repository.

Comments

Ignoring Files in Git — 2 Comments