Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If an AI-generated Git command or tool may have damaged your repository, stop anything still writing to it and make a complete copy before trying repairs. Then determine whether a branch or HEAD merely moved, useful objects became unreachable, or Git objects are missing or corrupt. Those cases require different recovery paths.

First, stop writes and preserve the repository

Stop the AI tool and any scripts, editors, or other processes that could continue changing files or Git metadata. Make a copy or archive of the complete working tree and Git data before attempting recovery. The Git user manual calls backups the first defense against repository problems and recommends backing up before manually replacing objects: Git user-manual.

The Git directory is often named .git, but linked worktrees and separate Git directories can use a different layout. Preserve the complete repository, not just the visible project files. If possible, carry out diagnosis and recovery on the preserved copy, leaving the original untouched.

Record the state and classify the failure

Before changing references or running repair commands, record the action that may have caused the problem, the current branch and HEAD, the output of git status, and any error messages. Avoid cleanup commands such as pruning while investigating: unreachable objects may contain recoverable work, and Git advises pruning only when the repository is quiescent.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The key distinction is whether Git lost a pointer to existing data or whether the object data itself is damaged:

  • A reference moved: a reset, rebase, checkout, or similar operation changed where a branch or HEAD points. The previous commit may still be available in a reflog.
  • A commit is unreachable: its object remains in the database, but no current reference points to it. git fsck may find it as a dangling or unreachable object.
  • Objects are missing or corrupt: a reference alone cannot restore them. You will need another copy, such as a backup, archive, or clone containing the needed objects.

Look for a moved branch tip with reflog

Git’s reflogs record local updates to reference tips; the HEAD reflog also records branch switches. They are often the quickest place to look after a bad reset or rebase. On the preserved copy, run:

git reflog

Inspect the relevant branch’s reflog as well as the HEAD history. Find the entry corresponding to the last known-good state, then verify the candidate commit and its tree before restoring anything. The git-reflog documentation describes how reflogs record reference movements.

Reflogs are local records, not a remote backup, and they can expire. Git’s documented default expiry periods are 90 days for reachable entries and 30 days for unreachable entries; repository configuration can change them, so these periods are not guarantees.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Preserve a verified commit on a recovery branch

Once you have identified and checked the right commit, create a separate branch pointing to it rather than immediately moving the damaged branch:

git branch recovery <commit-id>

Replace <commit-id> with the verified object ID. Git’s recovery guide uses this approach to preserve a recovered commit: Git Internals — Maintenance and Data Recovery. Keep the original branch in place while you compare files and history.

Find dangling or unreachable objects with fsck

If the reflog does not reveal the commit, run a full integrity check on the preserved copy:

git fsck --full

Git describes git fsck --full as checking connectivity and validity of the object database. Its output may report dangling or unreachable objects, missing objects, or hash mismatches. A dangling commit can be the root of recoverable history, but it is only a candidate: inspect it before creating a branch at it. The git-fsck documentation explains the command and its output.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Do not treat git fsck --connectivity-only as a complete content check. Git says this option avoids reading blob contents, so it will not detect corruption inside blobs. If you use git fsck --lost-found, know that it writes dangling objects into .git/lost-found; preserve a copy first. That output can help locate objects, but it does not determine which version is semantically correct.

Restore missing or corrupt objects from another copy

git fsck can identify problems, but it cannot recreate data that is absent. Git’s documentation says corrupt objects must be found in backups or other archives. A known-good clone, remote, or archive may contain the missing objects, but it may not contain unpushed local work or the exact state you need.

Preserve the damaged repository before fetching or copying objects from another source. Confirm what refs and objects the other copy contains, and compare them with the state you are trying to recover. The Git user manual says that a single missing blob may sometimes be repaired, while missing trees—and especially commits—are harder to recover. It treats manual object replacement as a last resort and recommends backing up first: Git user-manual.

Choose a recovery source that fits the failure

Recovery source Best suited to Main limitation
Reflog A branch tip or HEAD moved by reset, rebase, checkout, or a similar reference update Local history may have expired or been removed; it cannot recreate missing object data.
Dangling commits found by fsck A commit object that remains but has no reference pointing to it You must identify the right candidate; a dangling commit may not represent the intended state.
Remote clone, archive, or backup Missing or corrupt objects, or broader repository loss It may not contain unpushed local work or the exact damaged state.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Validate recovered work before changing the main branch

After creating a recovery branch, inspect its commit history and files. Compare the recovered tree with the work you expected to preserve, then run the project’s normal tests and checks. Those checks establish whether the project is usable; Git’s recovery guidance helps locate and preserve candidate commits, but it cannot determine whether the recovered application state is correct.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Only after you have confirmed the recovered state should you decide how to reintegrate it into the primary branch. Keep the recovery branch until that work is complete.

Prevent a second loss during recovery

Git’s garbage collection documentation explains that objects are retained while reachable from refs, the index, remote-tracking branches, and reflogs, among other places. Concurrent operations can still put unreferenced objects at risk, which is why stopping writers and avoiding premature pruning matter during recovery. See git-gc Documentation.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.