Common Local Git Errors: Choose a Safe Recovery
Distinguish states
Detached HEAD is not inherently an error; commits made there need a named reference if you want to retain them. A conflict is an unresolved integration, not necessarily corruption. An invalid revision can mean a typo, absent ref, unborn branch or unavailable history. A refused switch protects local work that would otherwise be overwritten.
Check before switching
The fixture creates topic with different notes.txt content, then edits main. Switching to topic fails. Inspect both working and staged differences; save the local change independently and decide whether to commit on the appropriate branch or use a reviewed stash. Stash normally omits untracked files unless requested, and applying it may conflict. Never use discard options merely to silence the warning.
Resolve or preserve
If HEAD is detached and the commit is valuable, create a new unused branch at the verified current commit. During an ordinary merge, inspect status and unmerged index entries. Resolve and stage the intended content, then complete the merge; or after preservation, abort the correct operation. Merge abort can fail to reconstruct preexisting dirty changes. Do not start another integration while one is active.
git rev-parse --verify HEADgit ls-files --unmerged# Only for a valuable detached commit, using a new branch name:git branch rescue-detached HEADCheck the result
For an invalid ref use git show-ref and rev-parse --verify with a known expected name. The lab verifies nonexistent-ref failure, a rejected switch with unchanged files and index, a rescued detached commit, and a clean-start merge abort. Compare actual state with the intended state; a successful exit alone is not enough.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Common Local Git Errors: Choose a Safe Recovery — knowledge check
Choose evidence-based troubleshooting and data-preserving recovery.
A switch refuses to overwrite local edits. What next?
A valuable detached commit exists. How to anchor it?
What does unmerged index evidence show?
What must be checked before merge abort?
An invalid revision is reported. What next?