How to set up a .gitignore file for common project types
· Category: Git
Short answer
Create a .gitignore file in your repository root and list patterns for files and folders Git should ignore. Common entries include node_modules/, .env, and build directories. For connecting to remote repositories after setup, see how to add a remote repository in Git. If you accidentally commit a file, see how to amend a Git commit.
Steps
- Create the file:
touch .gitignore - Add common patterns:
-
node_modules/-.env-dist/-*.log - Save and stage:
git add .gitignore - Commit:
git commit -m "Add .gitignore" - Verify with
git statusthat ignored files are excluded
Tips
- Use GitHub's gitignore templates for your specific language or framework
- Add
.gitignorebefore committing sensitive files - If you need to change history after a bad commit, see how to amend a Git commit