A Git repository is fundamentally a directed acyclic graph (DAG) of commit objects, where each commit points to its parent(s). When this structure becomes corrupted, Git can no longer traverse it, leading to various errors.
Corrupted Repositories
The Problem: Git is complaining it can’t find a specific object or a reference is broken. This often manifests as fatal: bad object <hash> or error: inflate: data stream error (incorrect data checksum).
Common Causes & Fixes:
-
Disk I/O Errors or Interrupted Operations: A
git gc,git push, orgit pullwas interrupted while writing objects to disk. This is the most common culprit for outright corruption.- Diagnosis: Run
git fsck --full. Look for output likedangling blob,dangling commit, ormissing <type> <hash>anderror: inflate: data stream error. - Fix: Often, simply re-running the operation that failed can fix it. If
git fsckreports missing objects, and you know they should be there (e.g., from a recent push), try fetching from a known good remote:
If the corruption is severe andgit fetch origingit fsckshows broken links, you might need to replace the corrupted object. This is risky. If it’s a local branch that’s broken, you might have to reset it:
This works because Git objects are immutable. If an object is corrupted, it’s usually a sign of a read/write error on disk or a partial write. Fetching from a remote re-downloads the object, replacing the bad copy. Resetting a branch bypasses the broken commit chain and recreates the branch pointer.# Find the hash of the last known good commit on that branch git reflog show <branch-name> # Reset the branch to that good commit git branch -f <branch-name> <good-commit-hash> # Then, try to re-apply any commits that were lost git cherry-pick <commit-hash-1> <commit-hash-2> ...
- Diagnosis: Run
-
Filesystem Corruption (Beyond Git): The underlying filesystem has errors, which Git, like any application, will encounter.
- Diagnosis: Run your operating system’s filesystem check tool (e.g.,
fsckon Linux/macOS,chkdskon Windows). Also,git fsck --full. - Fix: Repair the filesystem using the OS tools. After the filesystem is healthy, Git operations should resume. If Git still reports issues, try
git fsck --fullagain. If it shows dangling objects, you can prune them:
Filesystem repair ensures the storage medium is reliable.git prune git gcgit pruneremoves unreachable objects thatfsckmight have flagged as "dangling" but are no longer needed.git gcthen cleans up the repository.
- Diagnosis: Run your operating system’s filesystem check tool (e.g.,
-
Antivirus/Backup Software Interference: Real-time scanners or backup processes locking or modifying Git’s internal files (
.git/objects/,.git/refs/) during Git operations.- Diagnosis: This is hard to diagnose directly from Git errors. Look for intermittent issues, especially after a file is added or modified. Check your antivirus logs or backup schedules.
- Fix: Configure your antivirus and backup software to exclude your Git repository directories (especially the
.gitfolder) from real-time scanning and backups.
This prevents external processes from interfering with Git’s internal file operations, ensuring data integrity.# Add .git directory to exclusion list in your AV software # Configure backup software to skip .git directories
-
Incorrect Manual File Manipulation: Someone (or a script) directly edited, moved, or deleted files within the
.gitdirectory.- Diagnosis:
git fsck --fullwill likely report missing objects or broken symbolic links within.git/objects/. - Fix: The safest approach is to re-clone the repository. If that’s not feasible, try fetching from a remote:
If the corruption is limited to a specific local branch, resetting that branch as described in cause #1 can help. Re-cloning is the most robust fix because it rebuilds thegit fetch --all git remote prune origin # if origin is the only remote git gc.gitdirectory from scratch using a known good copy from the remote.
- Diagnosis:
-
Hardware Issues (RAM/SSD/HDD): Faulty RAM or storage devices can corrupt data as it’s written or read.
- Diagnosis: Persistent, widespread corruption across multiple operations and repositories.
git fsck --fullmight show varying errors on repeated runs. OS-level disk checks (fsck,chkdsk) might also report errors. - Fix: Run hardware diagnostics (e.g.,
memtest86+for RAM, manufacturer tools for SSD/HDD). Replace faulty hardware. After hardware is fixed, you’ll likely need to re-clone repositories or fetch from remotes to ensure data integrity.
- Diagnosis: Persistent, widespread corruption across multiple operations and repositories.
-
Git Bug (Rare): While uncommon, bugs in Git itself can lead to corruption, especially in edge cases or with specific configurations.
- Diagnosis: If you’ve exhausted all other possibilities, and the corruption is reproducible, check Git’s issue tracker or mailing lists for similar reports.
- Fix: Update Git to the latest stable version.
Newer versions often contain bug fixes that address such issues.# Example for Ubuntu sudo apt update && sudo apt upgrade git # Example for macOS with Homebrew brew upgrade git
The Next Error You’ll Hit: If you fix all corruption and git fsck passes cleanly, the next error you’ll likely encounter is a normal "nothing to commit, working tree clean" or a successful git status output.
Missing Commits
The Problem: You’ve lost a commit, or a branch seems to have diverged unexpectedly, showing fewer commits than you expect. This isn’t usually corruption but rather a misunderstanding or accidental re-pointing of references.
Common Causes & Fixes:
-
Accidental
git reset --hardorgit checkout <commit-hash>: You were on a branch, performed agit reset --hardto an older commit, or checked out a detached HEAD state and then made new commits. The original commits are still in the repository but are no longer reachable from any branch.- Diagnosis: Use
git reflog. This command shows a history of whereHEADand branch tips have pointed.
Look for the SHA-1 hash of the commit you believe is missing.git reflog - Fix: If you find the missing commit’s hash in the reflog, you can restore it:
The reflog is a local history of changes to your# Assuming the missing commit is 'abcdef1' git checkout -b recovered-branch abcdef1 # Or if you want to put it back on the current branch git reset --hard abcdef1HEADand branch pointers. Restoring a commit involves simply re-attaching a branch pointer to that commit’s SHA-1 hash.
- Diagnosis: Use
-
Incorrect
git rebaseorgit merge: A rebase or merge operation might have been aborted, or it might have resulted in a state where expected commits were unintentionally dropped or not integrated.- Diagnosis: Check
git logbefore and after the operation. If you suspect a specific rebase, checkgit reflogfor entries related torebase. - Fix: If a rebase was aborted, Git usually leaves you in a state where you can resume or abort it. If commits were lost during a successful rebase or merge, you might need to use the reflog to find the SHA-1 of the state before the operation and reset to it, then re-attempt the operation carefully.
This works by reverting the branch to its previous known good state and then attempting the operation again, allowing you to observe and correct any missteps.# Find the SHA-1 hash of the branch tip BEFORE the rebase/merge from reflog git reset --hard <sha1-before-operation> # Then try the rebase/merge again, paying close attention to the output
- Diagnosis: Check
-
Working on the Wrong Branch: You made commits, but you were on a different branch than you intended, or you switched branches before committing.
- Diagnosis: Use
git logto see the commit history. Check the branch name at the top of the log. - Fix: If the commits are on the wrong branch but are still reachable (i.e., you haven’t reset the correct branch), you can simply cherry-pick them to the correct branch or merge the incorrect branch into the correct one. If you accidentally switched branches before committing, the commits might be in your reflog.
This is about redirecting Git’s pointers. Cherry-picking copies specific commits, while merging integrates branches.# If commits are on 'feature-a' but should be on 'main' git checkout main git merge feature-a # Or if they are recent and on 'main' but you wanted 'feature-b' git checkout feature-b git cherry-pick <commit-hash-1> <commit-hash-2>
- Diagnosis: Use
-
Force Pushing Overwriting History: A force push (
git push -forgit push --force-with-lease) from another contributor (or yourself) overwrote a branch with a shorter history.- Diagnosis: Check the remote history using
git log origin/<branch-name>. Compare it with your localgit log <branch-name>. - Fix: If you or a colleague force-pushed, your local history might be outdated. Use
git fetchto get the latest remote state. If the desired commits are no longer on the remote branch, and they were local to your machine, they might still be in yourgit reflog.
This scenario requires communication. If a force push lost data, the lost commits are only recoverable if they exist in someone’s local reflog. Thegit fetch origin # If the remote branch is now shorter, and you know the SHA-1 of the missing commit locally: git reflog # Find the SHA-1 of the lost commit git checkout -b recovered-branch <lost-commit-sha1> # Then you might need to rebase or merge this back into the remote branch after discussiongit fetchupdates your view of the remote.
- Diagnosis: Check the remote history using
-
Accidental Branch Deletion: A branch was deleted locally or remotely without proper archiving.
- Diagnosis: Check
git branch -ato see local and remote-tracking branches. Checkgit reflogfor commits that were previously pointed to by that branch. - Fix: If the branch was deleted locally but the commits are still reachable from
HEADorreflog, you can recreate the branch:
If the branch was deleted remotely, you might need to recreate it locally and push it back up.# If the branch was deleted locally, and the commits are still on HEAD git checkout -b <branch-name> # If the branch was deleted and you need to find its last commit from reflog git reflog # Find the SHA-1 of the last commit on the deleted branch git checkout -b <branch-name> <last-commit-sha1>
- Diagnosis: Check
The Next Error You’ll Hit: Once you’ve recovered missing commits, the "next error" is usually reintegrating them correctly, which could lead to merge conflicts if other work has been done on the target branch.