Track Deleted Files with Git
Git provides several ways to track and manage deleted files in your repository. This guide will show you how to properly handle file deletions while maintaining your project’s history.
What You’ll Learn
- Removing files from Git
- Tracking deletions
- Recovering deleted files
- Best practices
- Common scenarios
Implementation Steps
-
Remove Files
# Remove file and stage deletion git rm filename.txt # Remove file but keep in working directory git rm --cached filename.txt # Remove directory recursively git rm -r directory/
- Stage deletions
- Keep local copies
- Remove directories
- Commit changes
-
Track Deletions
# Check deleted files git status # View deletion history git log --diff-filter=D --summary # Find deleted files git log --all --full-history -- "**/deleted-file.txt"
- Monitor deletions
- View history
- Track changes
- Verify status
-
Recover Files
# Recover from last commit git checkout HEAD~1 -- filename.txt # Recover from specific commit git checkout <commit-hash> -- filename.txt # Recover from stash git stash pop
- Restore files
- Check history
- Use reflog
- Verify recovery
-
Clean Up
# Remove untracked files git clean -f # Remove untracked directories git clean -fd # Preview clean git clean -n
- Clean workspace
- Remove debris
- Preview changes
- Verify cleanup
Best Practices
-
Before Deletion
- Review impact
- Check dependencies
- Backup if needed
- Document reason
-
During Deletion
- Use proper commands
- Stage changes
- Write clear messages
- Test after removal
-
After Deletion
- Verify changes
- Update documentation
- Notify team
- Monitor impact
-
Recovery Planning
- Keep backups
- Document process
- Test recovery
- Update procedures
Common Use Cases
-
Remove Sensitive Data
# Remove sensitive file git rm --cached secrets.txt git commit -m "Remove sensitive data"
-
Clean Up Project
# Remove build artifacts git rm -r --cached build/ git commit -m "Remove build artifacts"
-
Update Dependencies
# Remove old dependency git rm -r --cached node_modules/ git commit -m "Update dependencies"
-
Restructure Project
# Move and remove files git mv old/path/file.txt new/path/ git rm -r old/path/ git commit -m "Restructure project"
Advanced Usage
-
Filter Branch
# Remove file from history git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch sensitive.txt" \ --prune-empty --tag-name-filter cat -- --all
-
Interactive Rebase
# Clean up history git rebase -i HEAD~5
-
Submodule Cleanup
# Remove submodule git submodule deinit -f path/to/submodule git rm -f path/to/submodule rm -rf .git/modules/path/to/submodule
-
Large File Cleanup
# Remove large files git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch *.zip" \ --prune-empty --tag-name-filter cat -- --all
Common Issues and Solutions
-
Accidental Deletion
# Recover from reflog git reflog git checkout HEAD@{1} -- filename.txt
-
Untracked Files
# Clean untracked git clean -fd
-
Merge Conflicts
# Resolve conflicts git checkout --ours filename.txt git add filename.txt git commit -m "Resolve deletion conflict"
Conclusion
Proper file deletion management is crucial. Remember to:
- Use correct commands
- Track changes
- Document reasons
- Plan recovery
- Follow best practices
Next Steps
After mastering file deletion, consider:
- Learning about Git hooks
- Exploring Git workflows
- Understanding Git internals
- Setting up automated cleanup