How to ignore files in Git with .gitignore

Ignoring files in Git with .gitignore prevents unwanted files from being tracked, keeping your repository clean and avoiding sensitive data or build artifacts in version control. As the creator of CoreUI with 25 years of development experience since 2000, I’ve configured .gitignore files in countless projects to maintain clean repositories and prevent security issues with sensitive files. The most effective approach involves creating a .gitignore file in your repository root with patterns that match files and directories you want to exclude from Git tracking. This method ensures consistent ignore behavior across all contributors while maintaining repository cleanliness and security best practices.

Create a .gitignore file in your repository root with patterns matching files and directories to exclude from Git tracking.

# Create .gitignore file
touch .gitignore

# Add common ignore patterns
echo "# Dependencies" >> .gitignore
echo "node_modules/" >> .gitignore
echo "vendor/" >> .gitignore

echo "# Build outputs" >> .gitignore
echo "dist/" >> .gitignore
echo "build/" >> .gitignore
echo "*.min.js" >> .gitignore

echo "# Environment files" >> .gitignore
echo ".env" >> .gitignore
echo ".env.local" >> .gitignore
echo "config/secrets.yml" >> .gitignore

echo "# IDE files" >> .gitignore
echo ".vscode/" >> .gitignore
echo ".idea/" >> .gitignore
echo "*.swp" >> .gitignore

echo "# OS files" >> .gitignore
echo ".DS_Store" >> .gitignore
echo "Thumbs.db" >> .gitignore

# Stage and commit .gitignore
git add .gitignore
git commit -m "Add .gitignore file with common patterns"

This .gitignore file uses pattern matching to exclude common file types including dependencies, build outputs, environment variables, IDE configurations, and OS-generated files. The patterns support wildcards (*), directory indicators (/), and comments (#) for organization. Once committed, Git will automatically ignore matching files in current and future commits, keeping your repository focused on source code and preventing sensitive information from being accidentally tracked.

Best Practice Note:

This is the .gitignore pattern structure we use in CoreUI projects to maintain clean, secure repositories across all development environments. Always add .gitignore before making your first commit and review it regularly as your project grows to ensure comprehensive file exclusion.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author