On more than a few occasions I have unintentionally over-tracked files within a repository directory.
This usually happens when I’m speeding through a project and forget to properly update the .gitignore
file to exclude certain directories or files from being tracked by git before running something like git add -A
.
The most direct way I usually take to stop tracking certain files is to run:
git rm -r --cached <file(s)>
Why is over-tracking in git a problem? #
It’s generally a bad idea to take a kitchen-sink approach to just dumping everything in a directory into git for tracking.
Oftentimes, there are certain things that shouldn’t be tracked within a git repository. Things that often shouldn’t be tracked within a git repository include:
- Data files, especially if the data is sensitive or large.
- Temporary files, such as those ending in
*.tmp
. - Swap files, such as those ending in
*.swp
. .DS_Store
files on Macs, which is really intended to improve the local user experience. It does pose security risks, such as creating opportunities for possible breaches.
Adding too much to git tracking is also simply in-efficient. The approach I prefer with using git (and I imagine the approach that most developers favor) is to track all that is essential to a project, and no more.
How does over-tracking in git happen? #
If you’re anything like me, I might go through a sequence of steps like:
- Get excited about a project and fire up a directory to start building in right away.
- At some point, possibly after a good deal of work, I remember that I should probably track things in git. In comes the
git init
command, followed shortly bygit add -A
, thengit commit -am <some very informative commit message>
. - Realize that I forgot to set up a proper
.gitignore
to exclude certain directories or files from tracking.
This happens more often than I would like, but fortunately, git makes it relatively trivial to undo that error.