Skip to content

Ignore Files and Folders

Eric Bouchut edited this page Sep 4, 2018 · 12 revisions

Ignore Files and Folders

Edit the file:

  • .git/info/exclude to ignore files that exists only in your local repository. (for example temporary files generated by a local smoke test you are the only one to use). .git/info/exclude is not under version control. Use it to ignore files specific to your local configuration.

  • .gitignore to ignore files in your local repository and in any clone of your repository. (This file contains files (and folders) ignored by default for anyone cloning (or pulling from) this repository) .gitignore is under version control and shared by anyone using the repository. Once commited and pushed to the repository, file exclusion configured in .gitignore applies to whoever uses the repository.

The exclusion rule for a file/folder must be added in either of these files, before git begins tracking it. If you need to ignore a file after git started tracking it, see git update-index --assume-unchanged below.

You can also define a global gitignore file that will apply to whatever repository.

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.

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

Hide a File already Tracked by Git

You may sometimes need to ignore a file already tracked by git. Using the files .gitignore or .git/info/exclude won't work in this case but the following command may come in handy.

Hide a File

Run the following command to temporarily exclude a file from git tracking, pretending it never changed.
We hide this file from git.

git update-index --assume-unchanged FILE 

Unhide a File

Later on, if you need to commit changes to this file, simply use the opposite command to unhide this file from git:

git update-index --no-assume-unchanged FILE

Unhide all Hidden Files

If you want to unhide all hidden files:

git update-index --really-refresh

List Hidden Files

To list the files hidden from git, use this:

git ls-files -v | grep '^[a-z]' | cut -c3-

Aliases

The below aliases may come in handy if you intend to use the above commands on a regular basis or need an easier way to remember them without the syntax burden.

git config --global alias.hide       'update-index --assume-unchanged`
git config --global alias.unhide     'update-index --no-assume-unchanged`
git config --global alias.unhide-all 'update-index --really-refresh'
git config --global alias.hidden     '!git ls-files -v | grep '^[a-z]' | cut -c3-'

Clone this wiki locally