Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Ignoring Files in Git | More Advanced Interaction
Git Essentials
course content

Course Content

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

bookIgnoring Files in Git

Ignoring Files

Ignoring files in Git is crucial for several reasons:

  • Reduced Repository Size: Ignoring unnecessary files prevents them from being stored in the version control system, leading to smaller repository sizes;
  • Focus on Source Code: By excluding generated files or artifacts, your repository remains focused on source code and essential project files;
  • Security: Avoiding the inclusion of sensitive information, like API keys or passwords, enhances the security of your project.

The .gitignore File

The primary mechanism for specifying files to be ignored is the .gitignore file. This file, typically placed in the root of your repository, contains a list of file patterns that Git should ignore. Each line in the file represents a pattern for files or directories to ignore. For now, we'll simply use the names of the files.

Let’s first list all (including hidden) files and directories in our project directory:

If you are working on a macOS computer, you’ll most likely have the .DS_Store file, which is automatically created by the operating system. It makes no sense tracking it and committing, so let’s create the .gitignore file using the echo command and write the .DS_Store line to it to ignore this file:

Note

If .gitignore is not empty you should use the >> operator to append a new line with the filename to it.

Afterward, we can run the git status command and see that this file doesn’t appear in the list of untracked files:

Now, let’s add our .gitignore file and commit it:

The commit is successful, and the .DS_Store file is ignored.

How to properly add a file named handler.py to a NON-EMPTY .gitignore file?

How to properly add a file named handler.py to a NON-EMPTY .gitignore file?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
some-alt