How to Use .gitignore: Managing Files You Want to Exclude from Tracking
In the previous articles, we've learned the basic operations of Git and GitHub. However, when you run `git status` in a real project, have you ever been bewildered to see files automatically generated by the OS (like `.DS_Store` on a Mac), sensitive information like passwords, or a huge number of dependency files (like `node_modules`) show up, wondering, "Do I need to manage all of these?"
This article will detail how to use the "`.gitignore`" file to intentionally exclude files and folders that don't need to be, or shouldn't be, under Git's management. By using `.gitignore` correctly, you can keep your repository clean and manage your projects safely.
What is .gitignore? Why Do We Need to Exclude Files?
`.gitignore` is, as its name suggests, just a plain text file for listing files and folders that you want Git to "ignore." By placing this file in your project's root directory, Git will exclude any files or folders matching the patterns written in it from `git status` displays and from being targeted by `git add`.
So, why do we need to exclude files? There are three main reasons:
- 1. To Prevent Leaking Sensitive Information: Accidentally uploading configuration files (like `.env`) containing database passwords or API keys to a public GitHub repository can lead to serious security incidents.
- 2. To Exclude Automatically Generated Files: Configuration files automatically generated by the OS or editors (`.DS_Store`, `.vscode/`), a vast number of dependency folders generated by commands like `npm install` (`node_modules/`), and files generated during a build (`dist/`) can all be regenerated automatically in other environments, so they don't need to be included in version control.
- 3. To Keep the Repository Clean: By limiting version control to only the essential source code written by humans, the repository becomes cleaner and easier to navigate, making it easier for others to join the project.
Basic Syntax and Rules for .gitignore
Writing a `.gitignore` file is very simple. You just create a file named `.gitignore` in the root of your project (where the `.git` folder is) and list the names of the files or folders you want to exclude, one per line.
Basic Syntax Example:
# Lines starting with '#' are treated as comments
# Exclude by specifying a particular filename
debug.log
config_secret.php
# Exclude an entire directory (the trailing slash is optional)
node_modules/
dist
# Exclude all files with a specific extension
*.log
*.tmp
# Exception rule: Use '!'
# Ignore everything in the logs directory, but include important.log
logs/
!logs/important.log
You can also use wildcards (`*`), which allows for flexible configuration. A list of files that should be excluded in any project can be easily generated on sites like gitignore.io, so it's a good idea to make use of them.
In Practice! Steps to Create a .gitignore File and Ignore Files
Now, let's actually create a `.gitignore` file and confirm that unnecessary files are no longer tracked.
Step 1: Create the .gitignore file
First, create a `.gitignore` file in your project's root directory. Since the filename starts with a dot, it can be difficult to create with a GUI file explorer. The most reliable way is to run the following command in your terminal (or Git Bash).
touch .gitignore
Step 2: List the Files and Folders to Exclude
Open the created `.gitignore` file in a text editor and let's write down some items that are commonly excluded in web development.
# OS / Editor Files
.DS_Store
.vscode/
# Dependencies
node_modules/
# Log files
*.log
npm-debug.log*
# Environment variables
.env
After editing the file, don't forget to save it.
Step 3: Commit the .gitignore File Itself
This "exclusion rule" itself is an important setting that should be shared among all team members. Therefore, the `.gitignore` file itself should be included in Git's version control and committed.
git add .gitignore
git commit -m "Add .gitignore file"
Step 4: Verify That It Works
Let's check if files are actually being ignored. Create a file named `debug.log`, which we listed in our ignore list.
echo "This is a debug log." > debug.log
Now, try running `git status`.
git status
How does it look? `debug.log` should not appear in the "Untracked files" list and should be completely ignored. Success!
A Quick Note: What About Files That Have Already Been Committed?
A common pitfall for beginners is to "commit an important file and then frantically add it to `.gitignore`." However, `.gitignore` is only effective for files that have never been tracked by Git before.
If you want to remove a file that is already being tracked (e.g., `secret-key.php`) from Git's management, you need to remove it from Git's tracking list (the index) with the following command.
git rm --cached [filename]
This command is safe as it only "removes the file from Git's tracking," without deleting the file itself from your computer. After running it, don't forget to commit this change along with your `.gitignore` changes.
git commit -m "Stop tracking secret-key.php"