GIT Ignoring
-
What we’ll cover
The **.gitignore** file
Ignoring files
Ignoring directories
-
What is a .gitignore file?
A .gitignore file is a configuration file used to specify files and directories that should be ignored and not tracked by Git.
It allows you to exclude files from being included in Git repositories, making it especially useful for ignoring generated files, build artifacts, temporary files, and other files that shouldn’t be part of the version history.
Let’s see how to use a .gitignore file…
Create a .gitignore file:
If you don’t already have a .gitignore file in your Git repository, you can create one in the root directory. You can use a text editor to create the file, or you can use the touch command to create an empty file:
```touch .gitignore
-
### Edit the .gitignore file:
Open the **.gitignore** file in a text editor and specify the file patterns or directory paths that you want to ignore. You can use **wildcards** and **patterns** to match multiple files or directories.
Let's see some examples...
-
### Editing the .gitignore file: ignoring files
- To ignore a specific file, simply list the **filename**:
ignore_this_file.txt
- To ignore all files with a certain extension, use a **wildcard**:
*.log
-
### Editing the .gitignore file: ignoring directories
- To ignore all files in a specific **directory**, use a directory **path** followed by a **wildcard**:
logs/*
- To ignore all files in a **directory** and its **subdirectories**, use a double **wildcard**:
logs/**
-
### Editing the .gitignore file: ignoring directories
- To ignore a specific **directory** and its contents, specify the directory **path**:
build/
You can create rules for ignoring multiple files and directories by adding them to the **.gitignore** file. **Each rule should be on a separate line.**
-
### Save the .gitignore file:
Save your changes to the **.gitignore** file.
-
### Commit the .gitignore file:
You should commit the **.gitignore** file to your Git repository, so others working on the project also have the same ignore rules:
git add .gitignore git commit -m “Add .gitignore file” git push ``` - Once the .gitignore file is in place and committed, Git will automatically exclude the specified files and directories from being tracked or included in the version history. These ignored files will not show up in Git status or be part of Git commits. -
Pro Tip: Maintain a .gitignore file
It’s a good practice to create and maintain a .gitignore file for every project to avoid cluttering your Git repository with unnecessary or autogenerated files, and to keep your version control system focused on tracking the source code and important project assets.
-
