How to add a .gitignore file in Git?

To add a .gitignore file, just create a plain text file named .gitignore in your project root.

Inside, list all files or directories Git should ignore. For example:

Copy
Edit
*.tmp
build/
.DS_Store
Then run:


git add .gitignore  
git commit -m "Add .gitignore to exclude unwanted files"

This prevents those files from being tracked in future commits. I usually add .gitignore at the very start of a project.