
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 Make Git Forget About a Tracked File Which is Now in .gitignore
If you've ever tried adding files to your Git repository (say, config files that you didn't want tracked) only to have realized later that they shouldn't have been added in the first place, you might have created your .gitignore file. But somehow, you'll find that Git still does track them even when they're intended to be added to .gitignore. This is annoying but fortunately Git has a way to go and forget those tracked files forever. In this article, I will present a step-by-step guide to work through this common Git problem.
Why is Git Keeping Ignored Files?
Anything that goes into Git has its files added and then a record of changes made to its files, gets created in Git's index. But they only prevent new files from being added to the index by the .gitignore file. Once placed in .gitignore, it's not enough ? Git will still track the file unless you specify this command. For this, you'll have to tell Git to forget the tracked file.Step-by-Step Guide to Make Git Forget a Tracked File in .gitignore
Here are the steps to untrack a file that you've already committed and want to ignore:Step 1: Add the File to .gitignore
Add the file to .gitignore if you haven't already added the filename or pattern to .gitignore. In this way, Git won't touch the file in any future commits after we manage to forget it.
Open your .gitignore file and add the following line (replace file_name with the name of your file):
file_name
Step 2: Stop git from tracking the File
Second, you want to remove the file from Git's index, so Git can 'forget' the file. Remember that this command does not delete the file from your working directory, it only removes the file from tracking.git rm --cached file_name
Instead of file_name, use the name of the file you want to be forgotten with Git.
Explanation of the Command
git rm --cached removes the file from your index but not from your project folder. This means that Git will stop tracking the file, but that file is still somehow in your local directory.Step 3: Commit the Changes
After you've taken the file out of the index, we have to actually commit those changes to update the git repository. Use the following commands:git commit -m "Remove tracked file that is now in .gitignore"
Git knows that, since you are not tracking the file anymore, it will not get added to index anymore and thus it will tell Git in this commit: you want not to track this file any more.
Step 4: Push these Changes (If you are working with remote repositories)
If you're working with a remote repository (like GitHub, GitLab, etc.), take notice not to neglect pushing the changes for the remote repository to be updated.git push
To stop tracking the file and to update the remote repository as well, we just run this command.
Example: Ignoring Multiple Files Matching Patterns
To remove multiple files from Git, matching a certain pattern, add the pattern to .gitignore, and use git rm --cached with a wildcard.For example, if you want Git to stop tracking all .log files, add *.log to .gitignore:
*.log
Then, remove all .log files from the index with the following command:
git rm --cached *.log
Once that's done, commit and push the changes to conclude.
Important Note: Don't Lose Local Changes
If you want to make Git forget files, but still don't want to lose any local changes, make sure you've already committed any work on those files. Keeping your local files unharmed when removing files from the Git index and adding them to .gitignore is something that would hopefully never happen, however it's always a good idea to back up any important work.Summary of Steps:
- Add the unwanted file to .gitignore.
- Use git rm --cached file_name to remove the file from the Git index.
- Commit the changes with git commit -m "Remove tracked file that is now in .gitignore".
- Push the changes if you're working with a remote repository.
If you follow these steps, then you'll be sure that Git would stop tracking the files that you have already set in .gitignore.
Tip
If you have many files that need to be ignored after initially being tracked, a quick way to make Git "re-evaluate" the .gitignore is to remove all tracked files from the index and then re-add only the ones that are needed:
git rm -r --cached .
git add .
git commit -m "Reapply .gitignore settings"
The following command sequence deletes the index of Git temporarily and adds all the files back again based on the current .gitignore file so that it will not track any ignored file.
With these steps you are going to avoid tracking of unnecessary files into Git repository, which saves space and desclutter the place. Happy coding!