gitignore File

Types of git ignores

There are the 3 ways to specify a list of files to ignore.

Local Private Ignores

With local ignores, the files are only ignored for you in your local environment. Because this ignore list is not shared with other developers, the ignore list won't affect the environment of others. To use local ignore simple edit the .git/info/exclude file and add your ignore rules there. Also these rules only apply to the current GIT project.

#example, to edit using VIM editor
vim .git/info/exclude

Per Project Shared Ignores

Another way is to create a .gitignore file in your project. The rules in the ignore file applies to the folder in which it resides and any folders. Other folders may contain its own .gitignore file as well.

Local Global Ignores

This setting also applies to your local environment only. However, unlike the .git/info/exclude which applies to the current project. The global ignore files apply to every project. To use global ignores, we need to specify with file should be used as the global ignore.

#the name of this file does not need to be .gitignore
git config --global core.excludesfile path/your/ignore/.gitignore

GIT IGNORE FACT

A git ignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.
Facts:
1. A .gitignore  file can exists in every directory.
2. .gitignore in sub directory will override patterns in parent directory
3. lines starting with # represents a comment
4. It is not possible to re-include a file if a parent directory of that file is excluded.

PATTERNS AND RULES

#match foo.txt in current directory
foo.txt
#match the file foo.txt in dir1
dir1/foo.txt
#matches a file or directory named foo anywhere
foo
#matches a file or directory anywhere, same as above
**/foo
#matches file anywhere
**/foo.txt
#matches only directory named foo(Note the trailing \)
foo/

INCLUDE FILE FROM EXCLUDED DIR

#exclude files in directory
sites/*
#overrides the above and now includes sites/readme
!sites/readme

MATCH ALL FILES IN ALL DIRECTORIES

Use ** to match a file in all directories

match foo.txt regardless of directory name
**/foo.txt

 

Cookbook Category: 
Git