Setting up a “gitignore” file effectively makes certain files and directories invisible to Git tracking. This is one of the best ways to keep your working directory clean as you move files between different repositories and workstations or once you open a project for collaboration with others.
- How the Gitignore File Works
- Ignore a Single File
- Ignore Multiple Files of The Same Extension
- Ignore a Directory
- Mark Exceptions
- Saving Comments and Blank Lines
Below you will see a series of different example patterns you can use in various situations when crafting a Gitignore file or for use in an “exclude” file. Patterns can be mixed and matched to meet more varieties than can be covered in one article, so this list is not exhaustive, but it can be used as a starting point.
If you don’t need cPanel, don't pay for it. Only pay for what you need with our scalable Cloud VPS Hosting.
CentOS, Debian, or Ubuntu No Bloatware SSH and Root Access
How the Gitignore File Works
In this article, you sill see how to use the Git “ignore” feature as a file and not a command. When this file is placed inside of your working directory it will be read by Git before performing operations on your files. The
.gitignore (notice the dot) file is read line by line, which means that lines can overwrite each other; keep this in mind when troubleshooting your file:
Also note, your
.gitignore file has no effect on files that are already being tracked by Git. If you want to ignore a file you are currently tracking you will need to remove it from Git.
An Alternative To “Gitignore”
If you do not want to use a
".gitignore" file in the working directory of the project — for example, if you have particular files unique to your workflow that you want to ignore — you can write your ignore patterns into an “exclude” file located in your
For system-wide ignore patterns, you can customize the
core.excludesFile variable in your Git configuration with a file path to a file containing your ignore patterns.
[core] excludesFile = /path/to/excludes-file
Ignore a Single File
One of the most basic pattewrns for your Gitignore file involves ignoring one single, specific file. In this instance, imagine you want to ignore a file called “test.txt”. The pattern, on its own line, will match this:
Ignore Multiple Files of The Same Pattern
Likewise, if you want to ignore all files of the same pattern, like a file extension, for example, you can use an asterisk to indicate a pattern that will match of all files of that type:
The above pattern will ignore all files that end with a
.txt extension. This matching pattern is of course not limited only to file extensions. For example, if you want to match all files that begin with “res”:
Ignore a Directory
You may have a situation in which you want to ignore an entire directory (including all of its contents). For example, imagine you want to ignore a directory called “scratch”. The following pattern will match for this directory and every file and path beneath it:
There may also be situations in which you want to mark a file as exceptional. For example, if a pattern matches a file type but you want to retain a file of that type, you can denote this pattern exception with an exclamation point, as below:
Saving Comments and Blank Lines
To help keep ignore patterns organized, you can use blank lines and “#” at the start of a line. Any lines that begin with a “#” mark will be passed over, and so you can use those lines for comment.
Well done! You now know how to instruct Git on which files you want to ignore from version control. If you find there is a certain pattern you are having trouble with, or if you want advice on matching for patterns feel free to leave a comment below.