Reflog to Recover Lost Commits
Git reflog is a powerful tool that keeps track of all reference changes in your repository. This guide will show you how to use reflog to recover lost commits and restore your work.
What You’ll Learn
- Understanding reflog
- Viewing reflog
- Recovering commits
- Best practices
- Common scenarios
Implementation Steps
-
View Reflog
# View reflog git reflog # View specific reflog git reflog show HEAD # View branch reflog git reflog show branch-name
- Check history
- Find commits
- Identify changes
- Track actions
-
Recover Commits
# Recover from reflog git checkout HEAD@{1} # Create branch from reflog git branch recovery-branch HEAD@{1} # Reset to reflog git reset --hard HEAD@{1}
- Find lost commit
- Create branch
- Reset to commit
- Verify recovery
-
Advanced Recovery
# Recover specific file git checkout HEAD@{1} -- path/to/file # Recover with cherry-pick git cherry-pick HEAD@{1} # Recover with reset git reset --soft HEAD@{1}
- Recover files
- Use cherry-pick
- Soft reset
- Verify changes
-
Clean Up
# Expire old reflog git reflog expire --expire=now --all # Clean reflog git gc --prune=now
- Manage reflog
- Clean history
- Optimize storage
- Maintain repo
Best Practices
-
Before Recovery
- Check reflog
- Identify commit
- Plan recovery
- Backup if needed
-
During Recovery
- Use safe methods
- Create branches
- Test changes
- Verify content
-
After Recovery
- Test thoroughly
- Update docs
- Clean up
- Monitor changes
-
Prevention
- Regular commits
- Clear messages
- Safe operations
- Backup strategy
Common Use Cases
-
Accidental Reset
# Find reset in reflog git reflog # Recover from reset git reset --hard HEAD@{1}
-
Lost Branch
# Find branch in reflog git reflog | grep branch-name # Recover branch git branch recovery-branch HEAD@{1}
-
Deleted Commit
# Find commit in reflog git reflog # Recover commit git cherry-pick HEAD@{1}
-
Wrong Merge
# Find pre-merge state git reflog # Reset to pre-merge git reset --hard HEAD@{1}
Advanced Usage
-
Selective Recovery
# Recover specific files git checkout HEAD@{1} -- file1 file2 # Recover with patch git show HEAD@{1} > recovery.patch git apply recovery.patch
-
Branch Recovery
# Find branch tip git reflog | grep "checkout: moving to" # Recover branch git branch recovery-branch HEAD@{1}
-
Stash Recovery
# Find stash in reflog git reflog | grep "stash" # Recover stash git stash apply HEAD@{1}
-
Complex Recovery
# Create recovery branch git branch recovery HEAD@{1} # Cherry-pick commits git cherry-pick HEAD@{1}..HEAD@{3}
Common Issues and Solutions
-
Missing Reflog
# Check reflog settings git config --get gc.reflogExpire # Update settings git config gc.reflogExpire 90
-
Recovery Conflicts
# Resolve conflicts git checkout --ours file git add file git commit -m "Resolve conflicts"
-
Partial Recovery
# Recover specific files git checkout HEAD@{1} -- path/to/file # Create patch git diff HEAD@{1} HEAD@{2} > changes.patch
Conclusion
Reflog is a powerful recovery tool. Remember to:
- Check reflog first
- Use safe methods
- Test recovery
- Keep backups
- Follow best practices
Next Steps
After mastering reflog, consider:
- Learning Git internals
- Exploring Git workflows
- Understanding Git hooks
- Setting up backups