Sub topic 14
Habits that make recovery easy
This chapter assumes you can commit, branch, and switch in Git. It also assumes you have read the earlier chapters on reading git reflog, using selectors like HEAD@{1}, and recovering from git reset --hard. Here the goal changes from rescuing work to making rescue cheap.
Commit before you experiment
The reflog records moves of HEAD and branch tips. It does not record uncommitted changes. If a hard reset or a bad checkout discards edits that were never committed, the reflog has nothing to restore.
The simplest habit is to commit before any risky operation. A throwaway commit is enough, because you can reshape it later.
git add -A
git commit -m "WIP: before rebase"
git rebase mainIf the rebase goes wrong, the work is in a commit, and the reflog can find it.
Mark your starting point with a branch
A reflog entry is addressed by position or time, and both change as you keep working. A branch name does not. Before a rebase, a reset, or a history rewrite, create a backup branch.
git branch backup/before-rebase
git rebase main
# If the result is wrong:
git reset --hard backup/before-rebaseThe backup branch is a pointer, so it costs almost nothing. It also keeps the old commits reachable, so garbage collection cannot remove them. Delete it with git branch -d backup/before-rebase once you are satisfied.
Write down hashes you might need
Git prints a hash after some destructive commands, for example when you delete a branch. Copy it before you continue. Short notes in the terminal scrollback or a scratch file save you from searching the reflog later.
You can also look at the tip of a branch before touching it.
git rev-parse --short HEADInspect before you restore
When you do need the reflog, do not reset to the first plausible entry. Check it first.
git reflog
git show --stat HEAD@{1}
git log --oneline -5 HEAD@{1}Prefer a recovery that cannot destroy anything. Creating a branch at the old commit changes no existing branch, so you can compare the two and decide.
git branch recovered HEAD@{1}
git log --oneline recoveredA git reset --hard after a mistaken reset can discard uncommitted work in the same way the first one did. Check git status before you run it.
Recover quickly
Entries expire. By default, reachable entries are kept for 90 days and unreachable ones for 30 days. The reflog is also local, so a fresh clone has none of your history. If you notice a loss, recover it that day. Do not run git gc --prune=now or git reflog expire --expire=now --all while you are looking for something.
Summary of habits
| Habit | What it protects against |
|---|---|
| Commit before risky operations | Losing uncommitted changes, which the reflog cannot restore |
| Create a backup branch | Reflog positions shifting, and expiry of unreachable commits |
| Note hashes Git prints | Searching for a deleted branch tip later |
| Inspect with `git show` first | Restoring the wrong entry |
| Recover into a new branch | Overwriting the current state |
| Act soon | Expiry of old entries |
A safe workflow
Exercise
You are about to run an interactive rebase on a branch with uncommitted edits. Name two steps to take first, and explain why the reflog alone would not be enough.
Show answer
Commit the edits (a WIP commit is fine) and create a backup branch such as backup/before-rebase. The reflog records movements of HEAD and branch tips, not uncommitted changes, so edits that were never committed cannot be recovered from it. The backup branch also keeps the old commits reachable and gives you a stable name instead of a reflog position that shifts.
Exercise
After a mistaken reset, you find the right commit at HEAD@{3}. What is a safer first step than running git reset --hard HEAD@{3}?
Show answer
Run git show --stat HEAD@{3} to confirm the content, then git branch recovered HEAD@{3}. This creates a branch at that commit without moving or overwriting anything, so you can compare it with your current state before deciding.