···9696This is a destructive action. Therefore I included a safeguard by using the lowercase `-d` parameter. If your branches are not cleanly merged, git will not delete them. If you are sure you want to delete all those branches no matter what, use `-D` instead.
9797:::
98989999+## Remove tracked files with .gitignore
100100+101101+We’ve all been there: you create a `config.json` or a `.env` file, write some code, and commit it. Later, you realize that file should definitely not be in the repository, so you add it to your `.gitignore`.
102102+103103+The problem? Git is already tracking it. Adding a file to `.gitignore` only stops Git from tracking *new* files. To tell Git to "forget" a file it is already tracking without deleting it from your hard drive, you use the `--cached` flag:
104104+105105+```bash
106106+git rm --cached <file_path>
107107+```
108108+109109+If you accidentally committed an entire directory (like `node_modules` or `dist`), you can do it recursively:
110110+111111+```bash
112112+git rm -r --cached <folder_name>/
113113+```
114114+115115+If your repository has become a bit of a mess and you want to "reset" the tracking for everything to strictly follow your current `.gitignore` rules, run this sequence from the repository root:
116116+117117+```bash
118118+git rm -r --cached .
119119+git add .
120120+git commit -m "Refactor: apply .gitignore to all tracked files"
121121+```
122122+123123+This removes everything from the index and adds it all back. Because the files are now in your `.gitignore`, Git will simply skip over them during the `add` step, effectively untracking them.
124124+125125+:::caution
126126+If the file contained secrets, untracking it is not enough. The secret is still present in earlier commits and may already be on the remote, so rotate the secret and consider rewriting history as well.
127127+:::
128128+99129## Summary
100130101131**🛰️ Track Upstream Branch**
···112142> `git fetch -p && git branch -vv | awk '/: gone] / {print $1}' | grep -v '^\*$' | xargs -I{} git branch -d "{}"`
113143>
114144> A pipeline that identifies local branches whose remote counterparts have been deleted ("gone") and attempts to delete them safely.
145145+146146+**🧺 Untrack "Ignored" Files**
147147+> `git rm --cached <file>`
148148+>
149149+> Removes files from the Git index while keeping them physically on your disk. Essential for when you added a file to `.gitignore` after it was already committed.
115150116151## Resources
117152