.gitignore
- Ignore Unwanted Files in Git
A .gitignore
file tells Git which files or directories should not be tracked in a repository. This helps prevent unnecessary files—like logs, compiled binaries, and environment variables—from being committed to Git.
1. Creating a .gitignore
File
To create a .gitignore
file, run:
Then open it in a text editor and add file patterns to ignore.
2. Common .gitignore
Rules
🔹 Ignore Specific Files
📌 This ignores config.json
and secret.env
in the root directory.
🔹 Ignore All Files of a Type
📌 Ignores all .log
and .tmp
files.
🔹 Ignore Directories
📌 Ignores node_modules
, build
, and dist
directories.
🔹 Ignore Everything Except a Specific File
📌 Ignores everything in logs/
except logs/important.log
.
🔹 Ignore System Files
📌 Ignores macOS .DS_Store
and Windows Thumbs.db
.
3. Global .gitignore
(For All Repos)
To ignore files globally across all repositories, create a global ignore file:
Then add patterns inside ~/.gitignore_global
.
4. How to Check if Git is Ignoring a File?
Use:
If a file is ignored, it will show the matching pattern.
5. Example .gitignore
for a Flutter Project
📌 Prevents tracking of Dart tool files, build directories, and secret keys.
6. What If You Accidentally Tracked an Ignored File?
If a file is already tracked but is now in .gitignore
, you need to remove it:
Then commit the changes:
🔹 Conclusion
.gitignore
is essential for keeping your Git repository clean and secure. Always add a .gitignore
before pushing a project to GitHub! 🚀
Need a .gitignore
template for a specific project? Let me know! 😊