Git Ignoring
Effective version control involves managing which files and directories are tracked and which
are not. Git provides a powerful mechanism for this through the use of a
.gitignore
file and the gitignore
command. We'll explore various
ways to use the gitignore
command and create
.gitignore
files to tailor your Git repository to your specific needs.
1. Creating a .gitignore File:
The first step in ignoring files and directories is creating a .gitignore
file
in your Git repository. This file contains patterns that match files or directories you want
Git to ignore. Create or edit it using a text editor:
bashtouch .gitignore
2. Ignoring Specific Files:
To ignore specific files, add their names to the .gitignore
file. For example,
to ignore a file named example.txt
:
plaintextexample.txt
3. Ignoring Files with Wildcards:
You can use wildcards in .gitignore
patterns to match multiple files. For
example, to ignore all .log
files:
plaintext*.log
4. Ignoring Directories:
To ignore entire directories, add the directory names to the .gitignore
file.
For example, to ignore a directory named logs
:
plaintextlogs/
5. Ignoring Files in a Directory:
You can also ignore files in a specific directory. For instance, to ignore all
.tmp
files in the temp
directory:
plaintexttemp/*.tmp
6. Ignoring Files Based on Extensions:
To ignore files based on their extensions, use patterns like:
plaintext*.exe *.log
This example ignores all .exe
and .log
files.
7. Ignoring Files by Prefix:
If files have a common prefix, you can use:
plaintextbuild-*
This ignores files like build-1
, build-2
, etc.
8. Ignoring Hidden Files:
To ignore hidden files (those starting with a dot), use:
plaintext.*
This pattern ignores files like .config
, .gitignore
, etc.
9. Negating Patterns:
If you want to exclude specific files or directories from being ignored, you can negate
patterns using !
. For example:
plaintext*.log !important.log
This ignores all .log
files except important.log
.
10. Ignoring Comments:
You can add comments to your .gitignore
file using #
:
plaintext# Ignore backup files *.bak
11. Ignoring Whitespace Changes:
To ignore changes in whitespace, use the -w
option with gitignore
:
bashgit config core.whitespace -w
12. Global .gitignore:
You can set a global .gitignore
file for all your repositories. Create a global
.gitignore
file and tell Git to use it:
bashgit config --global core.excludesfile ~/.gitignore_global