Troubleshooting and Recovery
45 min`git reflog` is your safety net—it shows all the places your HEAD has been, enabling recovery from almost any Git mistake. Reflog records every HEAD movement, including resets, checkouts, and merges. Even if you lose a branch or reset incorrectly, reflog can help you find and recover commits. Understanding reflog enables recovery from many mistakes. Reflog is essential for Git troubleshooting.
`git reset --hard HEAD~1` moves HEAD back one commit and discards all changes, permanently removing the last commit and all uncommitted changes. This is destructive—use with extreme caution. `--hard` discards everything in the working directory and staging area. Understanding reset --hard enables undoing commits, but be careful—changes are lost. Reset --hard should only be used when you're certain you want to discard changes.
`git reset --soft HEAD~1` moves HEAD back one commit but keeps changes staged, allowing you to modify the commit or combine it with other changes. Soft reset undoes the commit but preserves all changes in the staging area. This is useful for amending commits or combining multiple commits. Understanding reset --soft enables safe commit modification. Soft reset is safer than hard reset.
`git clean -fd` removes untracked files and directories, cleaning your working directory. The `-f` flag forces removal, and `-d` includes directories. Use `git clean -n` first to see what would be removed (dry run). Understanding clean enables removing untracked files. Clean is useful for resetting working directory to a clean state.
Understanding these commands helps you recover from almost any Git mistake, from accidental commits to lost branches. Git provides powerful recovery tools, but you need to know how to use them. Reflog is the key to recovery—it records everything. Understanding recovery commands enables confidence in using Git. Git mistakes are usually recoverable if you know the right commands.
Other recovery techniques include using `git fsck` to find dangling commits, using `git log --all --graph` to visualize history, and creating backup branches before risky operations. Understanding recovery techniques enables handling various scenarios. Best practices include checking reflog when you think you've lost work, using `--dry-run` flags when available, and creating backup branches before risky operations. Understanding troubleshooting enables confident Git usage.
Key Concepts
- git reflog shows HEAD movement history for recovery.
- git reset --hard moves HEAD back and discards changes.
- git reset --soft moves HEAD back but keeps changes staged.
- git clean removes untracked files and directories.
- Understanding recovery commands enables fixing Git mistakes.
Learning Objectives
Master
- Using git reflog to recover from mistakes
- Understanding git reset modes and when to use them
- Using git clean to remove untracked files
- Recovering from common Git mistakes
Develop
- Understanding Git recovery mechanisms
- Confidently using Git knowing mistakes are recoverable
- Troubleshooting Git issues effectively
Tips
- Check git reflog when you think you've lost work—it's your safety net.
- Use git reset --soft when you want to undo commits but keep changes.
- Use git clean -n first to see what would be removed.
- Create backup branches before risky operations.
Common Pitfalls
- Using git reset --hard without checking what will be lost.
- Not checking reflog when you think you've lost commits.
- Using git clean without dry-run, removing important files.
- Not understanding reset modes, using wrong one.
Summary
- git reflog is your safety net for recovery.
- git reset has different modes for different needs.
- git clean removes untracked files.
- Understanding recovery commands enables fixing mistakes.
- Git mistakes are usually recoverable.
Exercise
Practice recovering from common Git mistakes using reflog and reset.
# Scenario: You accidentally reset to the wrong commit
git reset --hard HEAD~3
# Realize your mistake and check reflog
git reflog
# Find the commit you want to return to
# Let's say it's abc1234
# Reset to that commit
git reset --hard abc1234
# Alternative: Create a new branch from the lost commit
git checkout -b recovery-branch abc1234
# Now you can merge this back to main if needed