.gitignore
A .gitignore file specifies the files and directories that Git should ignore when tracking changes in a repository. This is particularly useful for excluding build artifacts, temporary files, dependencies, and sensitive information from version control. The .gitignore file should be located in the root directory of a repository.
Common .gitignore patterns
Below are common patterns typically included in .gitignore files:
# Ignore node modules directorynode_modules/# Ignore build output directoriesdist/build/# Ignore environment files.env.env.local# Ignore log files*.log# Ignore system files.DS_StoreThumbs.db# Ignore IDE specific files.idea/.vscode/*.sublime-project*.sublime-workspace
Creating a .gitignore file
Step 1: Create a new .gitignore file in the repository’s root directory:
touch .gitignore
Step 2: Open the file in the preferred text editor and specify the files and directories to ignore:
# Open with VS Codecode .gitignore# Open with Vimvim .gitignore
Note: Wildcards like ‘*‘ can be used for pattern matching and ‘#’ for comments in the .gitignore file.
Common .gitignore rules
- Specific file:
filename.txt
- File pattern:
*.log
- Directory:
node_modules/
- Nested directory:
**/logs/
- Negation (don’t ignore):
!important.log
Note: If ignored files have already been committed, remove them from Git’s tracking using
git rm --cached <file>
before .gitignore takes effect.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Git on Codecademy
- Course
Learn Git & GitHub
Use our beginner friendly Git course to integrate Git and GitHub and manage versions of your projects using Git branches.With CertificateBeginner Friendly4 hours - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours