11. Using .gitignore to Ignore Files

Version control is an essential tool for software developers and IT teams. Git, one of the most popular version control systems, allows teams to collaborate on projects efficiently. However, not all files in a project are required to be tracked by Git. This is where the .gitignore file becomes a valuable tool. In this section, we'll explore the purpose of .gitignore, how to create it, and use it to keep your repository clean and free of unnecessary files.

What is the .gitignore file?

The .gitignore file is a simple text document that instructs Git which files or directories to ignore and not track. When a file is ignored, it is not added to the repository, even if you use commands like git add . or git commit -a. This is useful for excluding temporary files, personal configuration files, dependency directories such as node_modules, and compiled files, which should not be shared with other collaborators or are not required to run the project.< /p>

Creating a .gitignore file

To create a .gitignore file, simply create a new text file in the root of your Git project and name it .gitignore (the dot at the beginning is important as it indicates that it is a hidden file on Unix-like systems). Inside this file you will specify the defaults for the files and directories you want to ignore.

.gitignore syntax

The syntax of .gitignore is straightforward. Here are some basic rules:

  • Blank lines are ignored.
  • Lines starting with # are considered comments and are ignored.
  • Global patterns are supported, such as * to match any sequence of characters and ? to match a single character.
  • To ignore an entire directory, place a slash (/) at the end of the directory name.
  • To negate a pattern (that is, include files or directories that would otherwise be ignored), place an exclamation point (!) at the beginning of the pattern.

Examples of Using .gitignore

Let's see some examples of how to use the .gitignore file:

# Ignore all .log files
*.log

# Ignore the build directory
build/

# Ignore the my_secret_file.txt file
my_secret_file.txt

# Ignore all files in the docs folder except the README.md file
docs/*
!docs/README.md

# Ignore all .txt files except LICENSE.txt
*.txt
!LICENSE.txt

Global and local .gitignore files

In addition to the project-specific .gitignore file, users can define a global .gitignore file that applies to all Git projects on their machine. To configure a global .gitignore file, use the following command:

git config --global core.excludesfile '~/.gitignore_global'

Then you can create the .gitignore_global file in your user directory and add the rules you want to apply globally.

Ignoring Already Scanned Files

If you add a file to .gitignore after it has already been tracked by Git (that is, after it has been added to the index), Git will continue to track it. To stop tracking a file that is already in the index, you must explicitly remove it with the command:

git rm --cached [file]

This will remove the file from the index but keep the local copy intact.

Good practices with .gitignore

When working with .gitignore, there are some best practices to follow:

  • Comment out your .gitignore to explain why certain files are ignored.
  • Keep .gitignore updated as new unnecessary files and directories are created.
  • Use .gitignore templates for specific programming languages ​​and tools. You can find examples on GitHub at https://github.com/github/gitignore.
  • Share .gitignore with your team to ensure everyone is ignoring the same file types.

Conclusion

The .gitignore file is a powerful tool in a developer's arsenal for keeping Git repositories clean and focused on just the essentials. By understanding and effectively using .gitignore, you can avoid including unnecessary and sensitive files, which helps maintain the integrity and security of your project. RememberEnsure that .gitignore is flexible and can be adjusted as needed to meet the specific needs of your project and team.

With this information, you are well equipped to create and manage the .gitignore file in your Git and GitHub projects, ensuring that only relevant files are tracked and shared with other collaborators.

Now answer the exercise about the content:

What is the purpose of the `.gitignore` file in Git?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image History view: diffs and logs

Next page of the Free Ebook:

12History view: diffs and logs

4 minutes

Obtenez votre certificat pour ce cours gratuitement ! en téléchargeant lapplication Cursa et en lisant lebook qui sy trouve. Disponible sur Google Play ou App Store !

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text