.gitignore

.gitignore

.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:

touch .gitignore

Then open it in a text editor and add file patterns to ignore.

2. Common .gitignore Rules

🔹 Ignore Specific Files

config.json secret.env

📌 This ignores config.json and secret.env in the root directory.

🔹 Ignore All Files of a Type

*.log *.tmp

📌 Ignores all .log and .tmp files.

🔹 Ignore Directories

/node_modules/ build/ dist/

📌 Ignores node_modules, build, and dist directories.

🔹 Ignore Everything Except a Specific File

logs/* !logs/important.log

📌 Ignores everything in logs/ except logs/important.log.

🔹 Ignore System Files

.DS_Store Thumbs.db

📌 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:

git config --global core.excludesfile ~/.gitignore_global

Then add patterns inside ~/.gitignore_global.

4. How to Check if Git is Ignoring a File?

Use:

git check-ignore -v <file>

If a file is ignored, it will show the matching pattern.

5. Example .gitignore for a Flutter Project

# Dart related .dart_tool/ .packages # Build outputs /build/ .generated/ # Secrets .env firebase_config.json

📌 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:

git rm --cached <file>

Then commit the changes:

git commit -m "Removed ignored file from repo"

🔹 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! 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close