
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Stop Tracking and Start Ignoring in Git
Git is an effective system for version control, but in some cases, there is information that is best left out of our version control trees entirely. They may be temp files, system files, or some confidential data that should not be committed. Read this article to learn how to untrack files that Git has been tracking and how not to let Git track new files in the future. In addition, we have explained in this article how you can use the git ignore feature to clean your work and avoid unnecessary files.
Why Ignore Files in Git?
Ignoring files in Git can help:
- Prevent sensitive data leaks: Credentials or configuration files fit best not into Git history.
- Reduce repository size: Previous temporary or generated record files can at times overcrowd your repository.
- Simplify version control: Leaving these files out removes clutter from your relatively simple repository.
Understanding the .gitignore File
The .gitignore file is a simple text file which will instruct Git which files to ignore. You usually save this file in the root directory of your project's repository. More or less each line in .gitignore is a regular expression or a string pattern that Git uses in order to determine which file or directory should not be tracked.
Here's a quick example of what a .gitignore file might look like:
# Ignore Python compiled files
*.pyc
# Ignore node_modules folder
node_modules/
# Ignore environment files
.env
Basic .gitignore Patterns
- Asterisk (*): Matches any string of characters.
- Question mark (?): Matches a single character.
- Slash (/): Matches directories.
How to Stop Tracking a File in Git
If git is tracking a file and you wish it to be ignored, then adding it to .gitignore will not suffice. Git will still follow changes to that file. In order to stop tracking it, you have to untrack it. Here's how:
1. Add the file to .gitignore: Note that you can open your .gitignore file and specify the filename or the pattern of the file.# Add this file to be ignored
sensitive_data.txt
2. Remove the file from tracking: Untrack the file by using the command git rm - cached. This command takes a file away from Git and the working tree but not from the disk space of your computer.
git rm --cached sensitive_data.txt
3. Commit the changes: After untracking the file, commit your changes.
git commit -m "Stop tracking sensitive_data.txt"
4. Verify the file is ignored: Now you should check that git ignores the file by typing git status. If it worked, it won't bring up the file on the list of files to be tracked.
Pro Tip: Always be careful when untracking files that have been saved and staged in Git because those files still exist in your repository. You can use git filter-branch or you can use third party tool BFG Repo-Cleaner to delete sensitive data from the repository history if required.
How to Ignore New Files in Git
If you do not want to let Git track new files from the start, you simply have to add them into the .gitignore file. Here's how to do it:
1. Open or create the .gitignore file: If your repository doesn't include this crucial file, be sure to add it to the root of your repository.
2. Add files or directories to ignore: You need to input the filenames or patterns into this .gitignore file.# Ignore all .log files
*.log
# Ignore database credentials
db_credentials.json
3. Save and commit the .gitignore file:
git add .gitignore
git commit -m "Add .gitignore file to ignore log files and db_credentials.json"
Common Mistakes to Avoid
- Not committing .gitignore: When you fail to create .gitignore, other teammates will not be in a position to understand which files to exclude.
- Ignoring files already being tracked: Just bear in mind that .gitignore only works for untracked files. If a file is already tracked, you will have to use git rm?cached.
- Using incorrect patterns: Check again your syntax of .gitignore file. For instance, if their plan is to ignore all those files that are of.log extension should simply write *.log instead of.log.
Advanced Tips for Ignoring Files in Git
1. Global .gitignore: If you want ignore files at every Git repository on your computer, configure global .gitignore file.
git config --global core.excludesfile ~/.gitignore_global
2. Ignoring tracked files in a local environment: But at times you may wish to ignore files locally without this having any impact on the rest of the team. You can use .git/info/exclude for this, this is like .gitignore, but for each local machine only.
Quick Reference Commands
Here's a summary of the key commands:
Command | Description |
echo filename > .gitignore | Add a file to .gitignore |
git rm --cached filename | Stop tracking a file without deleting it |
git add .gitignore | Stage .gitignore changes |
git commit -m "message" | Commit the .gitignore changes |
git config --global core.excludesfile | Set a global .gitignore |