Select every technology in your project and get a single, de-duplicated .gitignore you can copy or download.
In software engineering, version control is essential for tracking code changes, collaborating with team members, and maintaining a history of your project. Git, the industry-standard version control system, tracks every file in your repository by default. However, not every file in your project directory belongs in version control.
A **.gitignore** file is a plain text file placed in the root of your Git repository that explicitly tells Git which files and directories to ignore. Any file matching the patterns defined in your .gitignore will not be tracked, staged, or committed to your remote repository (such as GitHub, GitLab, or Bitbucket).
Failing to configure a proper .gitignore file can lead to several serious issues:
.env, credentials.json, id_rsa).node_modules, Python's .venv, or Java's .gradle), which dramatically increases clone and pull times..vscode/ or .idea/) or OS-specific files (like macOS ._DS_Store or Windows Thumbs.db), which vary from developer to developer and cause constant, unnecessary merge conflicts.dist/, build/, out/, or .log files), which are dynamically generated and do not belong in source control.To write effective gitignore rules, you must understand the pattern-matching syntax used by Git. Git uses globbing rules to match file paths. Here are the core syntax rules:
# are treated as comments and are not parsed./ specifies that it should match only directories. For example, node_modules/ will ignore the node_modules folder and all of its contents recursively.* matches zero or more characters. For example, *.log will ignore all files ending with .log in any directory.? matches exactly one character. For example, image?.png matches image1.png and imageA.png, but not image12.png.[a-z] match any single character within the specified range. For example, temp[0-9].txt matches temp1.txt but not tempA.txt.! prefix negates a pattern. Any file matched by a negated pattern will be tracked, even if it was previously ignored. For example, if you ignore all logs with *.log but want to track a specific log, you can add !important.log.** matches nested directories. For example, **/logs/*.log matches logs/error.log, src/logs/error.log, and src/components/logs/error.log.One of the most critical roles of a .gitignore file is preventing security breaches. According to security research, thousands of API keys and credentials are leaked on GitHub daily due to poor gitignore configurations.
To secure your repositories, always adhere to these best practices:
.env files: Always add .env and .env.local to your gitignore. Instead, commit a .env.example file that contains the environment variable keys but has the values blanked out, allowing other developers to set up their own local configurations..pem, .key, .p12, and .id_rsa files are ignored globally or locally..DS_Store or Thumbs.db) so you don't have to add them to every single project repository. You can set this up using: git config --global core.excludesfile ~/.gitignore_global.A very common point of confusion for developers is why Git continues to track a file even after it has been added to the .gitignore file.
**Crucial Rule: .gitignore only prevents untracked files from being added. It does NOT stop Git from tracking files that are already in the repository index.**
If you accidentally committed a file (like .env or node_modules) and then added it to your .gitignore, Git will continue to track changes to that file. To resolve this and stop tracking the file without deleting it from your local filesystem, follow these steps:
# 1. Stage all your current changes (ensure your working directory is clean)
git add . && git commit -m "Save current state before untracking"
# 2. Remove the file or folder from Git index recursively (retains local file)
git rm -r --cached .env
# Or to untrack a folder:
git rm -r --cached node_modules/
# 3. Add the files to your .gitignore file (if not already done)
echo ".env" >> .gitignore
# 4. Commit the untracking change
git add .gitignore && git commit -m "chore: untrack .env and node_modules"
Once these commands are pushed, the file will be removed from the remote repository (and from other developers' machines upon pulling), but will remain safely intact in your local directory.
Yes, you can place .gitignore files in any subdirectory of your repository. The rules in a subdirectory's .gitignore file apply only to files and folders within that directory and its subfolders, overriding or supplementing the rules defined in the root .gitignore file. This is highly useful for monorepos or multi-language projects.
The .gitignore file is committed to the repository and shared with all developers working on the project. The .git/info/exclude file, located inside your local .git folder, is private to your local clone. Rules added to exclude will ignore files only on your machine and will never be committed or shared with others.
If a file is matched by an ignore pattern but you explicitly want to stage and commit it, you can bypass the ignore rules using the force flag: git add -f path/to/ignored-file.txt. Use this with caution as it can easily lead to accidental commits of secrets or build outputs.