.gitignore files contain a list of files Git should ignore in a local project. A .gitignore file commonly appears in the main directory of a project. You can ignore single files, multiple files, or folders.
You may have files that you do not want to include in the main version of your Git repository. These files may contain configurations or local variables that are private and should not be seen by other collaborators.
That’s where the .gitignore file comes in. This file allows you to instruct Git to ignore files.
In this guide, we’re going to talk about what .gitignore is, how it works, and how you can write your own .gitignore file. Without further ado, let’s get started!
What is a .gitignore File?
A .gitignore file lists the files which Git will ignore. .gitignore files usually appear in the root directory of a project. Files tracked by Git are not ignored unless you remove them from a project.
Git when you add them to a commit. This means that every file, including configuration files and compiled code, will be added to a commit if you use the * operator. This operator stages all files into a commit.
Often, developers want to ignore output folders, hidden system files, and compiled code. For instance, if you have a .env file in a repository, you may want to hide it. This is because .env files usually contain API keys which you may not want to commit to a repository.
There is no ignore command you can use to ignore files. You need to make a list of all the files that you want to ignore in the .gitignore file.
How to Write a .gitignore File
Creating a .gitignore file is simple. Open up a command shell and create a text file called .gitignore in the root directory of a project. You can do this using the touch command:
We’ve created a .gitignore file. We do not need to set up our file with any configuration settings. Our next step is to add a list of the files we want to ignore. These files become untracked files, which is another word to describe a file ignored Git in a local repository.
Consider the following .gitignore file:
This file ignores two resources: all files in the node_modules directory and the .env file. We’ve ignored two specific resources in our local repository: a folder and a file.
Let’s say that we want to ignore multiple files or directories. We can do this by using the * wildcard.
Let’s ignore all .pyc files in a repository:
All files whose name patterns match *.pyc are ignored. The asterisk is the wildcard operator.
GitHub has created an excellent collection of gitignore files that you can use on their site. View GitHub’s collection of gitignore files.
Using Multiple .gitignore Files
You can use multiple .gitignore files in the same repository. This works because .gitignore is relative to the directory in which it is placed.
In our last example, we placed .gitignore in the root directory of our project. Let’s say that we want to ignore all the files that end in .pyc in the diff/ folder. We could do this by creating a file called .gitignore in the diff folder using the command line:
We would add the following rule to the .gitignore file:
When we go to commit code to our repository, Git will ignore any file in the diff/ folder which ends in .pyc.
This setup is best when you only need rules to apply to certain folders within a project. However, it can be confusing to have multiple .gitignore files in one project. An alternative is to be more specific when you state which files your project should ignore. If you are specific, you can keep all your rules in one .gitignore file. Consider this file:
This .gitignore is in the root directory of a Git project. It instructs Git to ignore all files ending in .txt in the project. It also instructs Git to ignore all files ending in .pyc which are in the diff/ folder.
How to Create a Global Git Ignore Rule
It can be time-consuming to define a .gitignore file for every project you create. What’s more, you’ll usually find that most of your projects use the same set of .gitignore rules.
You can set a global git ignore rule which will automatically set the gitignore rules for every repository on your system. This file is usually placed in your home directory of your operating system, such as /home/your_username or /Users/your_username.
Let’s create a global .gitignore file:
This command creates an empty .gitignore file in our home directory. Open up this file and add in whatever rules you want to set for all the Git repositories on your system.
Once you have configured your gitignore file, you can enforce its rules using the core.excludesFile property:
git config --global core.excludesFile ~/.gitignore
This command sets the rules in the ~/.gitignore file to apply to all Git repositories on a computer.
Be careful when you are defining a global git ignore rule. This is because the rules in a global .gitignore are set by default across all projects.
A global .gitignore file is best used for common files like .env which you will almost never want to commit to a git repository.
How to Ignore a Committed File
You cannot always anticipate which files you will want to ignore in a project. It may be the case that you have previously committed a file to a repository but now you want to ignore it.
Let’s ignore the file config.json. This file is already committed to our repository. We should start by adding an ignore rule to our .gitignore file:
echo config.json >> .gitignore
This command appends config.json to the end of our .gitignore file. Now we have to remove the file from our repository. We can do this using the git rm –cached command. This command will only remove a file from your repository. It will remain a local file on your computer.
git rm --cached config.json
Next time we create a commit, the config.json file will be ignored.
How to Commit an Ignored File
You can commit a file that has been ignored in your .gitignore file.
This may be useful if you are creating default config files that you want to commit to a repository that would otherwise be ignored. You can commit an ignored file using the -f flag when you are adding a file to the staging area:
git add -f config.json git commit -m "feat: Create default config file"
This code adds the file config.json to the staging area. It then creates a commit based on the contents of the staging area. You can learn more about the git add in our git add tutorial. You can read about the git commit command in our git commit tutorial.
The .gitignore file allows you to ignore files in a Git repository.
You may not want to include all the files in your local copy of a project in your commits. For instance, you may not want to commit compiled code, or system logs, or config files. To ignore files, you can specify which ones you want to ignore in .gitignore.
A project can contain multiple .gitignore files. You can override a .gitignore rule using the -f flag when you use the git add command.
Now you’re ready to use the .gitignore file like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Read more