Troubleshooting
Solve common Git and GitHub issues quickly: merge conflicts, bad commits, rejected pushes, detached HEAD, and PR check failures.
Simple Explanation (ELI5)
Most Git problems are recoverable. The trick is knowing where you are (status/log), what changed, and choosing the right safe command to fix it.
Technical Explanation
Troubleshooting is state management: branch state, working tree state, staging state, and remote divergence. Use inspection commands first, then apply minimal corrective action.
Issue 1: Merge Conflict
# Update branch git fetch origin git rebase origin/main # Conflict appears: edit markers <<<<<<< ======= >>>>>>> # Resolve file content manually # Mark resolved git add src/config/app.ts # Continue rebase git rebase --continue # If needed, abort git rebase --abort
Issue 2: Wrong Commit Message / Forgot File
# Add missing file git add src/new-file.ts # Update last commit git commit --amend -m "feat(api): add user settings endpoint" # If already pushed, update remote carefully git push --force-with-lease
Issue 3: Push Rejected (non-fast-forward)
git fetch origin git rebase origin/main # Resolve conflicts if any git push origin feature/my-branch
Issue 4: Detached HEAD
# Save current detached work into branch git checkout -b fix/detached-head-work # Return to normal branch flow git checkout main
Issue 5: Recover Lost Commit
# Inspect recent HEAD movements git reflog # Recover commit by hash git cherry-pick abc1234 # OR reset branch to that point git reset --hard abc1234
Quick Diagnostic Checklist
| Question | Command |
|---|---|
| Where am I? | git status |
| What changed? | git diff, git diff --staged |
| What history exists? | git log --oneline --graph --decorate |
| What remotes? | git remote -v |
| What branch state? | git branch -vv |
Real-world Use Case
During release prep, a developer accidentally force-pushed an outdated branch state. Team recovered all lost commits within minutes using reflog and protected branch backups, avoiding downtime and preserving audit history.
Interview Questions
Beginner
Concurrent edits to overlapping lines in the same file across branches.
Run git rebase --abort.
Recovering references to commits after resets/rebases/accidental moves.
It prevents overwriting remote updates you have not seen.
git status to inspect current repository state.
Intermediate
Soft keeps staged changes, mixed keeps working tree only, hard discards both staged and working tree changes.
Choose one side or regenerate artifact; manual merge markers are not possible.
To apply a specific fix commit to another branch without full branch merge.
Different runtime, env vars, dependency versions, or stricter CI scripts.
Refactor ownership boundaries and reduce parallel edits on shared hotspots.
Scenario-based
Use reflog to find missing commit, restore/cherry-pick it, and communicate before next push.
Split conflict resolution by subsystem, involve module owners, and run full regression tests.
Revert offending commit/PR immediately and apply corrected change in a new PR.
Update branch protection rules to current workflow job names.
Check reflog, recover previous HEAD commit, and reinforce safer workflow with stash/feature commits.
Summary
Most Git issues are recoverable with calm, state-first diagnosis. Learn inspection commands deeply and apply the smallest safe fix.