brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git rm with cached stops tracking a file but keeps it on disk

Git
~/tips/tip-git-rm-cached-stop-tracking

git commit -m "Stop tracking .env"

Removes a committed file from the repository while leaving your local copy in place.

Adding a file to .gitignore does nothing for files git already tracks. To make git forget one, you have to remove it from the index.

git rm --cached .env
echo ".env" >> .gitignore
git commit -m "Stop tracking .env"

--cached removes the file from the index only. Your working copy stays exactly where it is, and git status shows it as deleted for the next commit plus, until you ignore it, as untracked. For a folder, I’d add -r: git rm -r --cached build/.

After the commit, git stops looking at the file and the ignore rule finally does its job.

Two warnings. First, the file is removed from the repository, not from history. If it contained a password, it’s still in every older commit, so change the password. Second, for everyone else this is a normal deletion: when a teammate pulls your commit, git deletes the file from their working tree. For a local config file that everyone needs, tell people before you push, or give them a .env.example to copy from.

Further reading: git-rm documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link