.gitignore chỉ có tác dụng với file chưa được track.
File đã commit thì git tiếp tục track bất kể .gitignore — phải gỡ khỏi index:
bash
# Gỡ khỏi git nhưng GIỮ file trên disk (--cached):
git rm -r --cached node_modules/
git rm --cached debug.log
# Đảm bảo pattern đã có trong .gitignore:
echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore
git add .gitignore
git commit -m "chore: stop tracking node_modules and log files"Lưu ý:
- Thiếu --cached → git rm xóa luôn file thật trên disk
- File vẫn nằm trong history cũ — nếu là secret/credentials thì chưa đủ: phải rotate credentials + rewrite history (git filter-repo)
- Teammate pull về sẽ thấy file bị gỡ khỏi index của họ — file trên disk của họ giữ nguyên (thành untracked)