What is .gitignore?
A .gitignore file is a text file that tells Git which files or folders to ignore in a project. Ignored files are not tracked by Git, meaning they wonโt be staged or committed. This is useful for files that are generated by your computer or build process, such as log files, compiled code, or dependency folders like node_modules.
Problem Scenario
When you run git status, you see a long list of untracked files that you donโt want to commit. These could be system files, editor configuration files, or build artifacts.
git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
.DS_Store
logs/debug.log
build/
node_modules/
Committing these files would unnecessarily bloat the repository and could cause issues for other collaborators.
Solution
1. Create a .gitignore File
Create a file named .gitignore in the root directory of your Git repository.
You can create it using a text editor or a command line tool:
touch .gitignore
2. Add Patterns to .gitignore
Open the .gitignore file and add patterns for the files and directories you want to ignore. Each pattern should be on a new line.
# Ignore macOS system files
.DS_Store
# Ignore log files
logs/
*.log
# Ignore build output directory
build/
# Ignore dependency directories
node_modules/
#: Lines starting with a hash are comments.logs/: Ignores the entirelogsdirectory.*.log: Ignores any file with the.logextension (e.g.,debug.log,error.log).build/: Ignores thebuilddirectory.node_modules/: Ignores thenode_modulesdirectory.
3. Commit the .gitignore File
The .gitignore file itself should be committed to the repository. This ensures that the same ignore rules are applied for all collaborators.
git add .gitignore
git commit -m "Add .gitignore file"
Now, when you run git status, the ignored files will no longer appear in the untracked files list.
Ignoring Already Tracked Files
What if you accidentally committed a file that should have been ignored? Simply adding the file to .gitignore wonโt make Git stop tracking it.
You first need to tell Git to stop tracking the file using git rm --cached.
For example, to untrack the config.local file:
- Add
config.localto your.gitignorefile. - Run the command:
git rm --cached config.local - Commit the changes:
git commit -m "Stop tracking config.local"
The --cached option removes the file from Gitโs tracking, but it remains in your local directory.
Global .gitignore
You can also create a global .gitignore file for all your repositories on your system. This is useful for ignoring files specific to your operating system or editor.
- Create a file, for example,
~/.gitignore_global. - Configure Git to use this file:
git config --global core.excludesfile ~/.gitignore_global - Add any global patterns to this file.
Conclusion
Using a .gitignore file is essential for keeping your repository clean and focused on the important source code. It prevents unnecessary files from being committed and shared. Many templates for different programming languages and frameworks are available online to get you started.
Leave a comment