Gitignore



The gitignore file designates untracked files that Git should ignore. Files already tracked by Git are not concerned.

In some cases, you don't want Git to track and commit a file, or a bunch of files. So you can tell Git to ignore them.

Git classifies every file in the working directory as one of three things:

  1. Tracked: A Tracked file is a file that has been previously staged or committed.
  2. Untracked: An untracked file is a file that has not been staged or committed.
  3. Ignored: An ignored file is a file that we explicitly told Git to ignore.

Ignored files are generally built artifacts and machine-generated files. These files can be regenerated from your repository, so it should not be committed.


The .gitignore file

The .gitignore file is a special file where you can specify untracked files that Git should ignore.

Each line in a .gitignore file defines a pattern. To decide whether to ignore a path, Git normally checks gitignore patterns from multiple sources.

Generally .gitignore file is checked at the root of the Git repository.


How to ignore files manually

There is not explicit Git ignore command; rather, the .gitignore file must be edited and committed when you have new files you want to ignore.


Pattern format in .gitignore file

There are some rules when adding a pattern to the .gignore file. We can summarize them as follows:

  • Git ignores blank lines and lines that start with (#). You can put a backslash "" in front of the first hash for patterns that begin with a hash.
  • The patterns can be negated by starting the pattern with the prefix (!)
  • To avoid recursively add the forward-slash (/) at the start of the pattern.
  • To specify a directory add the forward-slash (/) at the end of the pattern.

Global .gitignore

Git allows creating a global Git ignore patterns for all local repositories by setting the Git core.excludesFie property. To create a global .gitignore file, run the commands as follows:

$ touch ~/.gitignore 
$ git config ---global core.excludesFile ~/.gitignore

Your home directory is a good place where you can put your global .giignore file.


Ignoring a previously committed file

To ignore a file that you've already committed, you need to delete the file from your repository and then add a rule to the .gitignore file. The commands will run as follows:

$ echo *.log >> .gitignore
$ git rm --cached test.log
rm 'test.log'
$ git commit -m "Ignoring test.log file"

Using the --cached option for the command git rm signify that the file will be deleted from your repository, but it will stay in the working directory as an ignored file.

If you don't specify the --cached option, the file will be deleted from your repository and your local file system.


Committing an ignored file

You can force an ignored file to be committed to the repository using the -f (or --force) option. the commands will run as follows:

$ git add -f test.log 
$ git commit -m "Adding an ignored file"

Listing the ignored files

Git gives the possibility to list all the ignored files, there are many ways to do that, but the most common way is the ls command.

The command to list ignored files run as follows:

$ git ls-files -i --exclude-standard

Or

$ git ls-files --ignore --exclude-standard 


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.