A .gitignore
file is a text file that specifies patterns of files that should be ignored by Git when adding, committing, or pushing changes to a repository.
The .gitignore
file is usually placed in the root directory of the repository and applies to all subdirectories unless overruled by another .gitignore
file in a subdirectory.
Each line in a .gitignore
file specifies a pattern of files or directories that should be ignored. The patterns are usually relative to the root directory of the repository and use *
as a wildcard to match any character.
For example, the following .gitignore
file ignores all files with the .class
extension and all directories named build
:
*.class build/Swww:ecruo.lautturi.com
To ignore a specific file or directory, you can specify its name or path.
For example, the following .gitignore
file ignores the file secret.txt
and the directory lib/vendor
:
secret.txt lib/vendor/
You can also use !
to negate a pattern and include a file or directory that would otherwise be ignored.
For example, the following .gitignore
file ignores all files with the .class
extension except for the file main.class
:
*.class !main.class
To create a .gitignore
file in your repository, you can create a new file named .gitignore
in the root directory of the repository and add the patterns that you want to ignore. You can then add the .gitignore
file to the repository using the git add
command.
For example:
$ touch .gitignore $ echo "*.class" >> .gitignore $ git add .gitignore $ git commit -m "Add .gitignore file"
Note that the .gitignore
file is not automatically applied to files that are already being tracked by Git. To ignore a file that is already being tracked, you will need to remove it from the repository using the git rm
command and then add the .gitignore
file .