Git performs a great job of noticing when new files are added to the working directory or when current files that are in the repository are modified in our working directory, but that’s not always desirable. To resolve this, today, let’s learn about how .gitignore works!
What is a .gitignore File?
Let us start by defining what a .gitignore file is. It’s a plain text file where each line contains a pattern for files/directories to ignore. Interestingly, it’s often checked into the very Git repository it affects.
The .gitignore file plays a crucial role in your Git repository. It tells Git which files or directories to ignore in a project. This is particularly useful for ignoring files that don’t need to be committed, like temporary files, logs, or files containing sensitive information.
Understanding how gitignore works is essential for maintaining clean repositories. It prevents unnecessary files from cluttering up the repository or causing conflicts between developers.
It is Git ignore
Here are more on how gitignore works. A .gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that’s where Git looks for it. When determining what to ignore, Git normally checks gitignore patterns from multiple sources, with the following order of precedence, from highest to lowest:
- Patterns read from the command line for commands such as
git ls-files
andgit read-tree
. - Patterns read from a
.gitignore
file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. - Patterns read from
$GIT_DIR/info/exclude
. - Patterns read from the file specified by the configuration variable
core.excludesFile
.
The patterns in the .gitignore file are used to determine which files and directories to ignore when making a commit. These patterns can include wildcards, making them more flexible. For example, the pattern *.log
would ignore all files with a .log extension. A leading slash /
will ignore the root directory, a trailing /**
will ignore all subdirectories, and a !
before a pattern will negate the ignore, including the file or directory in commits again.
By understanding how gitignore works, developers can better manage their repositories and improve their workflow. It’s an essential part of using Git effectively. Remember, a well-maintained .gitignore file can save you from accidentally committing sensitive or unnecessary files to your repository.
How Gitignore Files Work
Q: What if we don’t want to track those changes? What if it’s just a temporary file that we don’t care about or if it’s just a log file that’s constantly changing?
Instead of having Git bother us about these files, we need a way to tell Git to ignore them and not notify us when they change. If we create a file and put it in the root of our repository (the root of the project) called .gitignore, Git will read this file and use its rules when looking at the other files to make a decision on what should be tracked and what shouldn’t. That’s exactly what we’re looking for.
FURTHER READING: |
1. What are Git Concepts and Architecture? |
2. HEAD pointer in GIT: What You Need To Know? |
3. Hash Values (SHA-1) in Git: What You Need To Know |
Changes made to git ignored files will be ignored by Git. So we need to understand a little bit about the kind of rules that we can put in that file. The simplest thing is just to put a simple name of the file and that file would get ignored, but we can also use pattern matching, basically simple regular expressions like an asterisk, question mark, and character sets like:
- * ? [aeiou] [0-9]
So for example, if we want to ignore all files in the logs file directory that end in .txt, we can do that with logs/*.txt. You can also use negative expressions. For example, we could say ignore all files that end in .php but do not ignore index.php.
- logs/*.txt
- *.php
- !index.php
If we want to ignore all files in a directory or folder, then we can use a trailing slash after the name so assets/videos/ would ignore all files that are in that directory. We can also put comments in the gitignore file by just putting a # sign in front of any text that we want to be a comment in the gitignore file, maybe we want to provide the reason why we ignored something and any blank lines are simply going to be skipped.
- assets/videos/
- # This is just a comment so it will be ignored
Let’s try an example so we get a feel for it. Inside our repository, our working directory is clean. Let’s add a file to it that’s going to be a temporary file. Let’s call it access.log. It might be used to log all of the access to the website.
It would be a constantly changing file. We would always have changes to it every time we came to Git. We don’t want to commit those changes to the repository. They’re not important for us to keep track of. So instead we just want to tell Git to ignore this file.
In order to do that, we’re going to make another file in here and that file is going to be called .gitignore, and inside that file, we’re just simply going to put one line which is access.log.
If we save it and type git status in the command line, it will show us a new gitignore file. It’s not mentioning anything about access.log anymore. Access.log is being ignored.
We could add our gitignore file. It would then be part of our repository and anyone checking out our repository or working with it would also ignore the access.log. Before we commit that change, let’s come over and just add a few others to this.
The first line is a comment. We know that because it has the # sign in front of it. Empty lines are going to be ignored so we can skip down a line. The 3rd and 4th lines are saying that anything that’s a compressed file (.gz & .zip). The fifth line says anything that it’s in the log directory. The sixth line indicates anything that ends in .log and also a number zero through nine, that happens sometimes if we have something rotating our log so we have log 1, log 2, log 3, and so on.
Let’s imagine that we also had an assets directory and in there was another folder called videos. In the 7th line, we don’t want to keep those in our repository maybe because they’re such large files and it’s not really meant for us to track them. All files that are in that directory would be ignored except if we have assets/video/iphone_ with any number .mp4 in the 8th line. That’s going to give us a group of files (those will be kept).
It’s a list of rules that Git will use to know what to track and what not to track. Now our listed files are still there but Git is not going to bother us about it. It doesn’t matter if we make changes to it. Don’t forget to commit the .gitignore file into the repository.
By using the .gitignore file and following its rule, we could deliberately tell Git not to track any file that we don’t need tracking. It’s a really simple and easy but powerful little feature that Git is providing us.