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

  1. Create the file: touch .gitignore
  2. Add common patterns: - node_modules/ - .env - dist/ - *.log
  3. Save and stage: git add .gitignore
  4. Commit: git commit -m "Add .gitignore"
  5. Verify with git status that ignored files are excluded

Tips

  • Use GitHub's gitignore templates for your specific language or framework
  • Add .gitignore before committing sensitive files
  • If you need to change history after a bad commit, see how to amend a Git commit