Learn how to create, configure, and update a .gitignore file in your GitHub repository with our step-by-step guide to keep your repo clean and secure.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Create a GitHub Repository
First, sign in to your GitHub account and click the New repository button. Provide a repository name and description. Choose whether it will be public or private, then click Create repository.
Step 2: Clone the Repository Locally
Use Git to clone the empty repository to your local machine. Copy the repository URL from GitHub and run:
git clone https://github.com/your-username/your-repo.git
cd your-repo
your-username
and your-repo
with your actual GitHub username and repository name.your-repo
on your computer and sets it as the current working directory.
Step 3: Create a .gitignore File
In your project folder, create a new file named .gitignore
. This file tells Git which files or directories to ignore when committing.
# Create the .gitignore file
touch .gitignore
touch .gitignore
(macOS/Linux) or type NUL > .gitignore
(Windows).
Step 4: Add Ignore Patterns to .gitignore
Open .gitignore
in your text editor and list the files or directories you want Git to ignore. Each pattern should be on its own line. For example, to ignore log files, dependencies, and environment variables:
# Node.js dependencies
node\_modules/
# Production build output
dist/
# Environment variables
.env
# IDE or OS files
.vscode/
.DS\_Store
node_modules/
ignores all files in the node_modules
directory.dist/
ignores build artifacts..env
ignores environment variable files containing secrets..vscode/
and .DS\_Store
ignore editor and OS metadata.
Step 5: Verify .gitignore Is Working
Before committing, check which files are being tracked by Git:
git status
.gitignore
, remove it from Git tracking with:
git rm --cached filename.ext
filename.ext
with the name of the file or directory.--cached
flag removes the file from tracking while leaving it on your filesystem.
Step 6: Commit and Push .gitignore
Once your .gitignore
is configured, commit it and push to GitHub:
git add .gitignore
git commit -m "Add .gitignore with common patterns"
git push origin main
git add .gitignore
to stage the file.git commit
to record the snapshot in the repository history.git push
to upload your changes to the main branch on GitHub.
Step 7: Update .gitignore Over Time
As your project grows, you might need to ignore additional files. Simply edit the .gitignore
file, add new patterns, and repeat the commit and push process:
# Example: ignore log files
\*.log
git status
to confirm only desired files are tracked.git add .gitignore
and git commit
.git push
.
Step 8: Use .gitignore Templates
GitHub maintains a collection of .gitignore
templates for many languages and frameworks. You can copy from https://github.com/github/gitignore and paste the relevant file into your project. This ensures you start with best practices.
Node.gitignore
or Python.gitignore
..gitignore
.
Conclusion
Using a well-configured .gitignore
file keeps your repository clean by preventing unnecessary or sensitive files from being tracked. Regularly update it as your project evolves and leverage GitHub’s official templates to follow community conventions.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.