Trung BìnhGit iconGit

Vô tình commit file `.env` chứa credentials lên remote. Xử lý như thế nào?

Credentials đã push = credentials bị compromise. Rotate ngay lập tức — đây là ưu tiên số 1.

Bước 1: Rotate credentials (NGAY LẬP TỨC):
- API keys, DB passwords, JWT secrets → revoke và generate mới
- Không chờ xóa khỏi git — ai đó có thể đã crawl rồi

Bước 2: Xóa khỏi git history:

bash
# Dùng git filter-repo (khuyến nghị, thay thế filter-branch):
pip install git-filter-repo
git filter-repo --path .env --invert-paths

# Hoặc BFG Repo Cleaner (nhanh hơn cho large repos):
java -jar bfg.jar --delete-files .env
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --force --all

Bước 3: Thông báo team re-clone (sau force push, local repos của mọi người bị stale).

Bước 4: Thêm .env vào .gitignore:

bash
echo ".env" >> .gitignore
echo ".env.local" >> .gitignore
git add .gitignore && git commit -m "chore: add .env to gitignore"

Phòng tránh: pre-commit hook detect secrets (git-secrets, detect-secrets, Gitleaks).

GitHub secret scanning tự động notify nếu detect credentials trong push.

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git