Categories
git

Git Tutorial Part 3: Ignoring Files (Gitignore)

Git Ignoring Files

In most projects we have a set of files that we do not want to track these are usually logs or compilation or runtime files. In these cases we make use of:

.gitignore

To create a file listing to match the files we don’t want to track:

cat .gitignore
*.[oa]
*~

This tells git to ignore all files ending in .o or .a and all files that end with a tilde.

It s important to setup your .gitignore before you get going on the project so you don’t accidentally commit unnecessary files.

Gitignore Rules:

  • # : are comments
  • standard glob patterns work
  • Patterns ending in forward slash (/) specify a directory
  • You can negate a pattern by starting it with an exclamation point

Gitignore Manual Notes

 man git
Name

Gitignore: specifies intentionally untracked files to ignore.

Description

Files already tracked by git are not affected. Each gitignore file has a pattern, and have an order of precedence as follows:

  • Patterns read from command line
  • Patterns read from .gitignore in the same directory path and up, with those on a higher level of the directory tree being overridden by those below.
  • Patterns read from $GIT_DIR/info/exclude
  • Patterns read from the file specified by the config variable core.excludesfile.

Gitignore patterns should be version controlled and distributed to other repositories in the .gitignore file. Patterns specific to a single user should go into the $GIT_DIR/info/exclude file.

Files / patterns that should be ignored in all situations should go into the core.excludesfile in ~/.gitconfig

Pattern Format
  •  Blank lines serve as seperators for readability
  • A line starting with # is a comment
  • ‘!’ negates the pattern
  • Pattern ending in a slash ‘/’ will match a directory and those under it. Will not match a regulat file or symbolic link.
  • If the patterns does not contain a slash ‘/’ git treats it as glob patterns
  • If the patterns does contain a slash ‘/’ it is still treated as glob patterns
  • A leading slash matches files in the root directory
 Important Gitignore Notes

To ignore uncommited changes in a file that is already tracked:

git update-index --assume-unchanged

To stop tracking a file that is currently tracked:

git rm --cached

Taken from: Git Pro