![]() |
VOOZH | about |
The .gitignore file is a powerful tool in Git that allows you to specify which files and directories should be ignored by Git. This helps prevent unnecessary files, such as build artefacts and temporary files, from being tracked and cluttering your repository. In this article, we will explore how to create and configure a .gitignore file to keep your Git repository clean and organized.
Table of Content
Using a .gitignore file is crucial for several reasons:
To create a .gitignore file manually, follow these steps:
Step 1: Open your terminal or command prompt: Navigate to the root directory of your Git repository.
Step 2: Create the .gitignore file: Use the following command to create the file
touch .gitignoreOpen the .gitignore file: Use a text editor to open and edit the .gitignore file. For example, with nano:
nano .gitignoreThere are various templates available for different programming languages and frameworks that you can use as a starting point. GitHub provides a collection of .gitignore templates at github.com/github/gitignore.
Steps to use a template:
The .gitignore file uses simple pattern matching to specify which files and directories should be ignored. Here are some basic patterns:
Ignoring Specific Files: To ignore specific files, simply list their names:
file.txtIgnoring Specific Directories: To ignore entire directories, append a slash (/) to the directory name:
/node_modules/
/build/
Ignoring Files by Extension: To ignore all files with a specific extension, use a wildcard (*):
*.log
*.tmp
Ignoring Nested Files and Directories: To ignore files and directories at any level, prefix the pattern with two asterisks (**):
**/temp/
**/*.backup
Negating Patterns: To include a file that would otherwise be ignored by a previous pattern, prefix the pattern with an exclamation mark (!):
!important.log
!/keep/
Here are some common examples of patterns used in a .gitignore file:
# Logs
logs
*.log
# Dependency directories
node_modules/
# Build output
dist/
build/
# Environment variables
.env
# IDE files
.vscode/
.idea/
Once you have created and configured your .gitignore file, make sure to add and commit it to your repository:
git add .gitignore
git commit -m "Add .gitignore file"
git push origin <branch-name>