Skip to content

Ignore Files and Folders

Eric Bouchut edited this page Feb 19, 2020 · 11 revisions

Ignore Files and Folders

You can define the rules to ignore files in 3 files.

  • Repository gitignore file: .gitignore This one is located at the root of the repository.
    The rules it contains override the global gitignore and applies to the repository it is part of.
    Use it for rules that you want to share with others using this git repository.

  • Local gitignore file: .git/info/exclude This one contains rules that are not shared with others (ie. not under version control) and apply exclusively to your local repository (for example a temporary file generated by a local smoke test you are the only one to use). Once committed and pushed to the repository, these rules applies to whoever uses the repository.

  • Global Gitignore file: $HOME/.gitignore You may find yourself adding similar ignore rules in all of your repositories' .gitignore file and wonder if there is a way to define them globally. The global ignore file does that, the rules it contains applies to all of YOUR git repositories on this system.

You can use a different file name for the global gitignore file, like so.

git config --global core.excludesfile ~/gitignore.global

Github contains a list of per language gitignore file templates: https://github.com/github/gitignore/tree/master/Global Use it as an inspiration as to what to put in ~/gitignore_global.

If you need to ignore a file after git started tracking it, see git update-index --assume-unchanged below.

List ignored files

List the ignore/exclusion rules as defined in .gitignore and .git/info/exclude.

git status --ignored

 .agignore
 .bundle/
 .idea/
 public/docs/.DS_Store
 tags

If you have trouble remembering the above command, you use create this handy alias:

git config --global  alias.ignored  "status --ignored"

# From now on you can use the following instead
git ignored

Find the rule ignoring a File

When you do not see a file that you modified in git status output this means it is either unknown to git yet or most of the time it is ignored. In order to find which rule asks git to ignore a file you need to take a look at the 3 files mentioned above. But there is a recent handy git sub-command to do this: check-ignore.

git check-ignore -v test/models/.user_test.rb.un~
/Users/eric.bouchut/.gitignore_global:24:*.un~  test/models/.user_test.rb.un~

The ignore rule for the file test/models/.user_test.rb.un~ is defined in line 24 of /Users/eric.bouchut/.gitignore_global with the pattern *.un~.

Clone this wiki locally