Working with environment files in Git can be tricky — especially when you accidentally commit them, later add them to .gitignore
, and then lose them after switching branches or working in a detached HEAD
state. In this post, we’ll break down why this happens and how to recover the lost environment files, even after Git deletes them from your working directory.
The Scenario
Let’s say you’re working on an Angular project. At some point:
- You accidentally commit sensitive environment files (
src/environments/environment.ts
) - Later, you realize the mistake and add them to
.gitignore
- You continue editing those files locally — but they are no longer tracked by Git
- One day, while inspecting an older commit (using
git checkout <commit>
), Git switches to a detachedHEAD
state and removes ignored, untracked files from your working directory - You return to
main
, and your local changes are gone
Why Did Git Delete the Files?
- Once a file is added to
.gitignore
and no longer tracked, Git treats it like any other untracked file. - When switching to a commit or another branch (especially in a detached
HEAD
state), Git may clean untracked files to keep the working directory consistent. - If you hadn’t committed or backed up the modified environment file, Git has no record of your changes, and they are gone from disk.
Step-by-Step Recovery Guide
1. Check If File Exists in a Previous Commit
Use git log
or git rev-list
to find the last commit that tracked the file:
git log -- src/environments/environment.ts
Or:
git rev-list -n 1 HEAD -- src/environments/environment.ts
Once you find a commit hash where the file existed, you can restore it:
git checkout <commit-hash> -- src/environments/environment.ts
This will restore the file as it was in that commit into your current working directory.
2. Check Git Stash (If You Used It)
If you had stashed your changes before switching commits:
git stash list
git stash show -p stash@{0}
git stash pop
3. Check Your Local File System or IDE History
Some IDEs (like VS Code, IntelliJ, WebStorm, etc.) keep local history of files even after deletion.
- In VS Code: Press
F1
→ “Local History: Show Entries for Active File” - In IntelliJ/WebStorm: Right-click in file explorer →
Local History
→Show History
4. Recreate Manually (if nothing else works)
If no Git version or backup exists, you’ll need to manually re-create the file.
Tip: Ask a teammate to share the latest version or reconstruct from build artifacts (if applicable).
Preventive Measures
1. Always Backup Before Ignoring Files
Before adding sensitive or config files to .gitignore
, make sure to:
- Commit one last clean version if it’s okay to be versioned temporarily
- Backup locally or save in a safe config management system
2. Use Environment Variables Instead
Avoid hardcoding secrets in environment.ts
. Use Angular’s environment configurations with .env
files and process.env
via custom builders or environment injection.
3. Use .git/info/exclude
for Personal Ignores
Instead of modifying .gitignore
(shared with the whole team), you can ignore files locally:
echo "src/environments/environment.ts" >> .git/info/exclude
This protects your local untracked files from accidental deletion.
Summary
Situation | Solution |
---|---|
File was committed, now ignored, and got deleted | Use git log or git rev-list to find a commit and restore with git checkout |
File was never committed, only edited locally | Check IDE local history or file backups |
You used stash |
Recover using git stash pop |
You need to ignore the file going forward | Use .git/info/exclude instead of .gitignore |
Final Thoughts
Git is powerful — and sometimes a little too smart. It tracks what you tell it to, and when you stop tracking something, it won’t protect it anymore. So when ignoring sensitive files like environment.ts
, make sure you understand the consequences and always have a recovery plan.