Understanding the Git Architecture: Working Directory, Staging, and History
Git's internal architecture is structured around three primary trees: the Working Directory (your local files on disk), the Staging Area / Index (the snapshot prepared for the next commit), and the Git Repository (HEAD) (the immutable commit graph).
Mastering Git recovery requires understanding how commands like git reset, git restore, and git reflog transition data between these three tiers.
Comparison: git reset vs. git revert vs. git restore
| Command | Rewrites History? | Safety Level | When to Use |
|---|---|---|---|
| git reset --soft | Yes (Local HEAD) | ๐ข Safe | Undoing local unpushed commits to re-stage or fix code. |
| git revert <hash> | No (Creates inverse commit) | ๐ข Safe | Reversing buggy commits that have already been pushed to public/shared branches. |
| git restore --staged | No | ๐ข Safe | Unstaging files accidentally added with git add .. |
| git reset --hard | Yes | ๐ด Destructive | Completely nuking uncommitted work and resetting to a known clean commit. |
Frequently Asked Questions
How long does Git Reflog retain deleted history?
By default, Git preserves reachable reflog entries for 90 days and unreachable entries for 30 days before the internal git gc garbage collector prunes unreferenced objects.
What is git push --force-with-lease?
--force-with-lease is a safer alternative to raw --force. It checks that the remote branch has not received any new commits from teammates before overwriting history, preventing accidental code loss.