Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

What a Git Commit Stores & Deleted History

· 6 min read · Home Tech

In this article
  1. What Exactly Is Inside a Commit?
  2. How Git Stores Objects: Blobs, Trees, and Hashes
  3. What Happens When You Delete History?
  4. How to Recover Deleted Commits
  5. Why Deleting History Is Dangerous (and When It's Necessary)
  6. How to Permanently Purge Deleted History
  7. Frequently Asked Questions

A Git commit stores a permanent snapshot of your entire project at a specific point in time, along with metadata like the author, date, and a message describing the change. When you delete history (e.g., with git reset, git rebase, or git branch -D), the commits aren't immediately erased—they become unreachable and are eventually garbage-collected, but they can often be recovered until that happens.

What Exactly Is Inside a Commit?

Each commit is a compressed object stored in the .git directory. It contains:

  • Tree object: a pointer to the root directory structure, which itself points to blobs (file contents) and other trees (subdirectories). This tree represents the full state of your project at that commit.
  • Parent commit(s): the SHA-1 hash of the previous commit (or multiple parents for a merge). This creates the chain that forms your history.
  • Author and committer: name, email, and timestamp for each (they can differ, e.g., when someone rebases).
  • Commit message: the text you write when you commit.
  • Optional GPG signature: if you sign commits, the signature is stored here.

Importantly, a commit does not store diffs. It stores the complete snapshot. Git computes the difference between a commit and its parent only when you ask for it (e.g., git show or git log -p). This design makes Git fast and robust—every commit is self-contained.

How Git Stores Objects: Blobs, Trees, and Hashes

To understand commits, you need to know about the underlying object model:

  • Blob: the binary content of a file. It has no filename or metadata—just the bytes.
  • Tree: a directory listing that maps filenames to blobs (or other trees) and includes file modes (executable, symlink, etc.).
  • Commit: as described above, points to a tree and parent(s).

Every object is identified by a SHA-1 hash (40 hex characters) computed from its content. This means identical content always produces the same hash, which is how Git deduplicates files. If you have the same file in 100 commits, it's stored once as a blob, and each tree just points to it.

When you run git commit, Git creates a new tree object that reuses existing blobs for unchanged files and creates new blobs for changed files. Then it creates a commit object pointing to that tree and the current HEAD as parent.

What Happens When You Delete History?

Deleting history usually means making commits unreachable. This happens with commands like:

  • git reset --hard HEAD~2 – moves the branch pointer back, abandoning the last two commits.
  • git rebase --onto – rewrites commits, creating new ones and leaving old ones dangling.
  • git branch -D – deletes a branch, making all its commits unreachable (unless they're also on another branch).

When a commit becomes unreachable, it's not immediately deleted. It stays in the object database, and you can recover it with git reflog (which tracks branch movements) or git fsck --lost-found (which finds dangling commits). The reflog keeps entries for a default of 90 days (configurable with gc.reflogExpire). After that, or if you run git gc with aggressive pruning, Git's garbage collector permanently removes unreachable objects—but only after they've been unreachable for a certain time (default 2 weeks for loose objects, 30 days for packed ones).

How to Recover Deleted Commits

If you accidentally delete a commit, you can often get it back:

  1. Check git reflog – it shows a history of where HEAD and branches pointed. Find the SHA of the commit you lost.
  2. Create a new branch or reset to that SHA: git branch recover or git reset --hard .
  3. If the reflog doesn't have it (e.g., after a clone or a fresh repo), run git fsck --lost-found to list dangling commits and blobs.

Important: if you've already run git gc with --prune=now, or if enough time has passed, the objects may be gone for good. Also, if you've pushed to a remote and then force-pushed to remove the commits, the remote may still have them in its reflog (if it has one), but you can't access that from your local repo.

Why Deleting History Is Dangerous (and When It's Necessary)

Deleting history is risky because it rewrites the shared narrative of a project. If others have based work on the commits you delete, they'll have divergent histories and will need to reconcile. Common reasons to delete history:

  • Removing sensitive data (passwords, API keys) that was committed.
  • Cleaning up a messy branch before merging (via rebase).
  • Reducing repository size by removing large files from history (using git filter-branch or BFG).

But for most everyday work, you should avoid rewriting history that has been pushed. Instead, use git revert to create a new commit that undoes the changes—this keeps the history intact and safe for collaboration.

How to Permanently Purge Deleted History

If you truly need to erase commits (e.g., for security), you must force Git to drop them:

  1. Remove all references (branches, tags, reflogs) to the commits.
  2. Run git reflog expire --expire=now --all to clear reflogs.
  3. Run git gc --prune=now --aggressive to delete unreachable objects immediately.

This makes the objects unrecoverable from your local repo. However, if you've pushed to a remote, you must also force-push the cleaned history and, if necessary, contact the remote host to purge its copies. Even then, anyone who cloned the repo before the purge may still have the old commits locally. So true deletion is nearly impossible once distributed.

Frequently Asked Questions

Does a commit store a full copy of every file?

Yes, conceptually. Each commit points to a tree that references blobs for all files. But Git compresses and deduplicates: unchanged files are stored only once, and Git uses delta compression in packfiles to store changes efficiently. So it's not as wasteful as it sounds.

Can I recover a deleted commit after running git gc?

If you ran git gc without --prune=now, objects older than the default expiration (2 weeks) are removed, but recent ones may survive. If you used --prune=now, they're gone immediately. The safest bet is to check the reflog and fsck before running gc.

What's the difference between git reset and git revert?

git reset moves the branch pointer and can discard commits, rewriting history. git revert creates a new commit that undoes the changes of a previous commit, leaving history intact. Use revert for shared branches, reset for local, unpublished work.

Why does my .git folder get so big even after deleting files?

Because Git keeps every historical version of every file. Deleting a file in a new commit doesn't remove the old blobs—they're still referenced by previous commits. To shrink the repo, you must rewrite history to remove those blobs, then run git gc.

Is it safe to delete history on a personal project?

If the project is only local and you're sure you don't need the old commits, it's safe. But even then, you might lose work you later realize you need. It's wise to keep a backup branch or tag before purging.