Course Content
Git Essentials
Git Essentials
Ignoring 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.
Thanks for your feedback!