Module 1 — Code Tab¶
Lesson 17: Git Reflog and Repository Recovery¶
Filename: docs/github/module-01-code/lesson-17-git-reflog-recovery.md
Suggested branch: docs/add-git-reflog-lesson
Level: Advanced
Practice repository: github-deep-dive-training
1. Learning objectives¶
By the end of this lesson, you should be able to:
- Understand Git references and HEAD.
- Understand how Git tracks reference movements.
- Use
git reflogto inspect previous Git states. - Recover commits after
git reset. - Recover deleted branches.
- Recover work after accidental rebases.
- Understand detached HEAD.
- Distinguish
git reset,git revert, andgit restore. - Recover from incorrect merges.
- Understand Git object retention and garbage collection.
- Build safe recovery workflows.
2. Understanding Git references¶
Before learning recovery, you need to understand how Git identifies commits.
A commit has a unique identifier called a commit hash.
Example:
Output:
a91d4e2 feat: add authentication
b82c1a5 fix: correct login validation
c73d2b1 docs: update README
d64e9f0 Initial commit
Each commit points to its parent commit or parents.
Git uses references to identify important commits.
2.1 What is HEAD?¶
HEAD represents your current position in the repository.
Normally, HEAD points to the branch you currently have checked out.
Example:
Inspect HEAD:
Inspect the HEAD reference:
Expected:
Get the current commit:
2.2 What is a branch?¶
A branch is a movable reference pointing to a commit.
Example:
When you create another commit on feature, the branch reference moves forward.
Branches are not complete copies of your repository. They are lightweight references.
2.3 What happens when you switch branches?¶
Git updates HEAD to point to main.
HEAD now points to feature.
Git records these movements in the reflog.
3. What is Git Reflog?¶
git reflog records changes to local Git references.
It is particularly useful when you accidentally:
- Reset a branch.
- Delete a branch.
- Move HEAD to another commit.
- Rebase incorrectly.
- Lose track of a previous commit.
- Change branches and forget where you were.
The reflog records reference movements, not a complete history of every file modification.
Basic command:
Example output:
a91d4e2 HEAD@{0}: commit: feat: add authentication
b82c1a5 HEAD@{1}: reset: moving to HEAD~1
c73d2b1 HEAD@{2}: commit: feat: add login page
d64e9f0 HEAD@{3}: checkout: moving from feature to main
Interpretation:
| Entry | Meaning |
|---|---|
| HEAD@{0} | Most recent HEAD movement |
| HEAD@{1} | Previous HEAD movement |
| HEAD@{2} | Two movements ago |
| HEAD@{3} | Three movements ago |
Important: Reflog entries are local and expire according to Git's reflog expiration settings. They are not automatically shared with other developers.
3.1 Useful reflog commands¶
Show HEAD history:
Show a specific branch's reflog:
Show all references:
Show a more detailed log:
Show recent entries:
Inspect a previous commit:
Inspect the reflog configuration:
4. Understanding git reset¶
git reset moves the current branch reference to another commit.
It has three main modes:
--soft--mixed--hard
Understanding their differences is essential for recovery.
4.1 Soft reset¶
What happens?
- HEAD moves backward one commit.
- The branch reference moves backward.
- Changes from the removed commit remain staged.
- Your working files remain unchanged.
Example:
Before:
After:
Use cases:
- Combining commits.
- Rewriting a recent commit.
- Changing a commit message.
4.2 Mixed reset¶
This is the default reset mode.
What happens?
- HEAD moves backward.
- The staging area is updated.
- Changes from the removed commit remain in the working directory.
- Changes become unstaged.
4.3 Hard reset¶
What happens?
- HEAD moves backward.
- The staging area changes.
- Tracked working-tree files are updated to match the target commit.
- Uncommitted changes to tracked files can be discarded.
Warning: This operation can destroy uncommitted work. Do not experiment with it in a real project.
Before using it, consider creating a backup branch or patch.
4.4 Comparison¶
| Operation | HEAD | Staging area | Working directory |
|---|---|---|---|
--soft |
Moves | Keeps changes staged | Keeps changes |
--mixed |
Moves | Resets | Keeps changes unstaged |
--hard |
Moves | Resets | Resets tracked files |
5. Exercise environment: Create a disposable recovery repository¶
Do not perform destructive recovery exercises directly on your main training branch.
Create a separate local repository.
Configure Git identity if necessary:
Create the first commit:
Create a second commit:
echo "Authentication module" > auth.txt
git add auth.txt
git commit -m "feat(auth): add authentication module"
Create a third commit:
echo "Order management module" > orders.txt
git add orders.txt
git commit -m "feat(orders): add order management"
Inspect:
You should have three commits.
6. Recovery scenario 1 — Recover after accidental hard reset¶
Imagine you accidentally execute:
Your latest two commits are no longer referenced by the current branch.
First inspect:
You may only see the initial commit.
Do not panic.
Run:
Example:
abc1234 HEAD@{0}: reset: moving to HEAD~2
def5678 HEAD@{1}: commit: feat(orders): add order management
ghi9012 HEAD@{2}: commit: feat(auth): add authentication module
You can recover the previous branch position.
Option A — Move the current branch back:
Option B — Create a recovery branch:
Then inspect:
Professional recommendation: Prefer creating a recovery branch first when you are unsure which commit contains the desired work.
7. Recovery scenario 2 — Recover a deleted branch¶
Imagine you created:
Then committed changes:
echo "Payment module" > payment.txt
git add payment.txt
git commit -m "feat(payment): add payment module"
Return to main:
Accidentally delete the branch:
The branch name disappears.
But the commit may still be recoverable.
Inspect:
Find the commit containing the payment module.
Then recreate the branch:
Switch to it:
Verify:
You have recovered the branch reference.
Important: A deleted branch is not the same as immediately deleted commit objects. Recovery is possible while the relevant commit remains available.
8. Recovery scenario 3 — Recover from an accidental rebase¶
Imagine you have:
You start a rebase:
You encounter conflicts and accidentally finish the rebase with an incorrect result.
Inspect:
Look for entries similar to:
HEAD@{0}: rebase (finish)
HEAD@{1}: rebase (pick)
HEAD@{2}: rebase (start)
HEAD@{3}: checkout: moving from main to feature
Identify the reference before the rebase started.
Create a recovery branch before changing anything:
Inspect:
If the original feature history is correct, you can restore the branch after verifying the result.
For example:
This resets the local feature branch. Do not do this blindly if the branch has already been pushed or other developers have based work on it.
9. Recovery scenario 4 — Detached HEAD¶
Detached HEAD means HEAD points directly to a commit instead of pointing to a branch.
Example:
Or:
You can inspect and test the historical commit.
However, if you create commits while detached, those commits are not automatically attached to a named branch.
Create a branch immediately if you want to preserve new work:
This creates a branch at your current commit.
Inspect:
Important: Detached HEAD is not an error. It is a valid Git state.
10. Recovery scenario 5 — Incorrect merge¶
Imagine you merge the wrong branch:
There are two possible situations.
Situation A: Merge is not committed yet¶
If Git reports conflicts:
You can abort the merge:
This attempts to restore the pre-merge state.
Situation B: Merge commit already exists¶
Inspect:
If the merge is already committed and has not been pushed, you may restore the previous branch position using a carefully selected reset.
For example:
Inspect ORIG_HEAD first:
If the merge has already been pushed to a shared branch, prefer a revert:
The -m 1 option selects the first parent as the mainline parent.
Verify the merge's parent ordering before using this command.
11. git reset vs git revert vs git restore¶
These commands solve different problems.
11.1 git reset¶
Moves the current branch reference.
Use when you need to change local commit history.
11.2 git revert¶
Creates a new commit that reverses the changes introduced by an earlier commit.
Use when you need to undo changes without rewriting shared history.
Example:
E reverses the changes introduced by C.
The original commits remain in history.
11.3 git restore¶
Restores file contents.
Restore a working-tree file:
Unstage a file:
Restore a file from a specific commit:
Be careful: restoring a file can discard local changes to that file.
11.4 Comparison¶
| Command | Main purpose | Rewrites existing history? |
|---|---|---|
git reset |
Move branch reference | Yes, depending on use |
git revert |
Create an undo commit | No |
git restore |
Restore file contents | No commit history change |
12. Understanding ORIG_HEAD¶
Git sometimes stores the previous position of HEAD in ORIG_HEAD.
This can be useful after:
- Merge operations.
- Reset operations.
- Rebase operations.
- Other operations that move HEAD.
Inspect it:
Recovering to it:
Only do this after checking that ORIG_HEAD represents the state you want.
It is not a permanent backup. Later Git operations may replace its value.
13. Git object storage and garbage collection¶
Git stores repository content as objects.
Important object types:
| Object | Purpose |
|---|---|
| Blob | File content |
| Tree | Directory structure |
| Commit | Snapshot metadata and parent relationships |
| Tag | Annotated tag metadata |
Inspect objects:
Inspect an object:
Git also performs garbage collection.
Garbage collection can prune unreachable objects after their retention period.
This is why recovery is not guaranteed indefinitely.
Do not run aggressive pruning commands while attempting recovery.
For example:
can remove unreachable objects immediately.
Avoid this during recovery exercises.
14. Useful recovery commands¶
git reflog
git reflog show --all
git reflog --date=iso
git log --oneline --graph --all
git show <COMMIT_HASH>
git branch -a
git branch recovery/backup <COMMIT_HASH>
git switch recovery/backup
git reset --soft HEAD~1
git reset --mixed HEAD~1
git revert <COMMIT_HASH>
git restore <FILE>
git show ORIG_HEAD
git fsck --full --no-reflogs --unreachable
git fsck can help locate unreachable objects when reflog entries are unavailable.
Do not immediately reset or delete anything you discover. Inspect candidate commits first.
15. Recovery best practices¶
Before a potentially destructive operation:
- Inspect your working tree.
- Check staged and unstaged changes.
- Create a backup branch when appropriate.
- Record important commit hashes.
- Understand whether the branch is shared.
- Avoid rewriting shared history without coordination.
Useful commands:
For valuable uncommitted work, consider:
Or create a patch:
Remember that ordinary git diff does not capture untracked files.
16. Practical exercises¶
Complete these exercises in your disposable git-recovery-lab repository.
Exercise 1 — Recover from reset¶
- Create three commits.
- Run
git reset --hard HEAD~2. - Inspect
git log. - Inspect
git reflog. - Identify the lost commit.
- Create a recovery branch.
- Verify the recovered files.
Expected result: Your previous work is available on the recovery branch.
Exercise 2 — Recover a deleted branch¶
- Create
feature/payment. - Add a file.
- Commit it.
- Switch to main.
- Delete the feature branch.
- Find its commit in reflog.
- Recreate the branch.
- Verify the commit.
Expected result: The branch is recreated at its previous commit.
Exercise 3 — Recover after rebase¶
- Create a main branch with two commits.
- Create a feature branch.
- Add two commits to the feature branch.
- Rebase the feature branch.
- Inspect the reflog.
- Identify the original feature position.
- Create a recovery branch.
- Compare both histories.
Expected result: You can inspect both the original and rebased histories.
Exercise 4 — Detached HEAD¶
- Checkout an earlier commit.
- Confirm detached HEAD.
- Create a new commit.
- Create a branch at that commit.
- Switch to the new branch.
- Verify the commit is preserved.
Expected result: The new commit belongs to a named branch.
Exercise 5 — Revert a shared commit¶
- Create a feature commit.
- Merge it into a test branch.
- Push the test branch if you have a remote configured.
- Revert the feature commit.
- Inspect the resulting history.
Expected result: The original commit remains in history and a new commit reverses its changes.
Exercise 6 — Recovery investigation¶
Simulate a recovery incident:
A developer reports that they accidentally deleted a feature branch after several commits. They cannot remember the branch name or exact commit hash.
Your task:
- Inspect all available reflogs.
- Inspect all branches.
- Search for relevant commit messages.
- Identify the candidate commit.
- Inspect the candidate commit's contents.
- Create a recovery branch.
- Verify the recovered files.
- Document the recovery steps.
Expected result: Produce a short incident report explaining how the branch was recovered.
17. GitHub repository practice¶
Return to your training repository.
Create the lesson branch:
Create:
Paste this lesson into the file.
Also create:
The recovery runbook should contain:
- Incident description.
- Initial repository state.
- Recovery objective.
- Commands used.
- Identified commit hashes.
- Recovery branch name.
- Verification steps.
- Final result.
- Lessons learned.
Suggested recovery runbook template¶
## Git Recovery Runbook
## Incident
Describe what happened.
## Initial state
Describe the current branch, HEAD, and working-tree status.
## Investigation
Commands used:
- git status
- git reflog
- git log --oneline --graph --all
## Recovery
Describe the recovery procedure.
## Verification
Explain how you confirmed that the files and commits were recovered.
## Result
Document the final state.
## Lessons learned
What should the developer do differently next time?
18. Commit and push¶
Check:
Stage:
git add docs/github/module-01-code/lesson-17-git-reflog-recovery.md
git add docs/github/module-01-code/recovery-runbook.md
Commit:
Push:
Create a pull request:
Title¶
After review and merge, create an annotated tag:
git switch main
git pull origin main
git tag -a module-01-lesson-17 -m "Module 1 Lesson 17: Git Reflog and Recovery"
git push origin module-01-lesson-17
Check first that the tag does not already exist.
19. GitHub Release¶
Create a release using:
Tag: module-01-lesson-17
Release title¶
Suggested release notes:
## Module 1 — Lesson 17
## Topics
- Git references and HEAD
- Git reflog
- Soft, mixed, and hard reset
- Deleted branch recovery
- Rebase recovery
- Detached HEAD
- Incorrect merge recovery
- Reset vs revert vs restore
- ORIG_HEAD
- Git object storage
- Garbage collection
- Recovery best practices
## Practical exercises
- Recover commits after reset
- Recover deleted branches
- Recover after rebase
- Recover detached HEAD commits
- Revert shared commits
- Investigate a recovery incident
## Documentation
- lesson-17-git-reflog-recovery.md
- recovery-runbook.md
20. Completion checklist¶
- [ ] Understand HEAD and Git references.
- [ ] Understand branch references.
- [ ] Understand git reflog.
- [ ] Recover a commit after reset.
- [ ] Recover a deleted branch.
- [ ] Recover from an incorrect rebase.
- [ ] Understand detached HEAD.
- [ ] Recover an incorrect merge.
- [ ] Understand reset, revert, and restore.
- [ ] Understand ORIG_HEAD.
- [ ] Understand Git objects and garbage collection.
- [ ] Complete the six practical exercises.
- [ ] Create the recovery runbook.
- [ ] Create a pull request.
- [ ] Merge the lesson.
- [ ] Create the annotated tag.
- [ ] Publish the GitHub Release.
21. Official documentation¶
- Git Reflog: https://git-scm.com/docs/git-reflog
- Git Reset: https://git-scm.com/docs/git-reset
- Git Revert: https://git-scm.com/docs/git-revert
- Git Restore: https://git-scm.com/docs/git-restore
- Git Branch: https://git-scm.com/docs/git-branch
- Git Fsck: https://git-scm.com/docs/git-fsck
- Git Garbage Collection: https://git-scm.com/docs/git-gc
- Git Checkout: https://git-scm.com/docs/git-checkout
22. Next lesson¶
Module 1 — Lesson 18: Git Internals — Objects, Trees, Blobs, Commits and Packfiles¶
Planned topics:
- How Git stores files internally.
- Blob objects.
- Tree objects.
- Commit objects.
- Tag objects.
- Object hashes.
- The Git object database.
- Loose objects.
- Packfiles and delta compression.
- The index and staging area internals.
- Inspecting objects using
git cat-file. - Exploring
.git/objects. - Understanding Git storage efficiency.
The objective is to understand what happens internally when you execute Git commands, rather than treating Git as a collection of commands to memorize.