How to git-ignore Cached Code files and all Hidden Directories Recursively in a Git Repository!
Sometimes, while coding, we forget to add a .gitignore
file that includes list of files or directories to ignore when pushed to a remote repository.
Then we forgetfully pushes the code and see there are unnecessary cache files or hidden directories/files present in the online repository, which causes pain for other collaborators.
If you accidentally have done so, just follow these steps, and you are good to go.
Steps
- Check how many hidden directories are there using the following command.
find . -type d -name '.[^.]*' -prune -exec echo rm -rf {} +
once you find out, and have no issue with deleting each of the listed hidden directories, remove the
echo
from the above command.find . -type d -name '.[^.]*' -prune -exec rm -rf {} +
Careful if you already have a
.git
directory! - Delete the
__pycache__
folders from all the directories in the same wayfind . -type d -name '__py[^.]*' -prune -exec rm -rf {} +
- Finally, create a
.gitignore
file and add the followings:**/__pycache__/ .* !/.gitignore
Pro Tips
To avoid adding a particular named file or directory, for example., to ignore.DS_Store
files in every subfolder for all repositories in the computer, do the following:
$ echo .DS_Store >> ~/.gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global
Now, if you do git add --all
, it will not automatically include these particular directories created again while working in the local machine.
Leave a comment