Mastering Git: How to Resolve Merge Conflicts and Recover Lost Commits using Reflog
This comprehensive guide walks you through resolving painful Git merge conflicts manually and, more importantly, how to use the powerful git reflog to recover commits that seem permanently lost.
Direct Diagnostic & Remediation Summary
- To resolve a conflict: Manually edit the conflicting files marked by HEAD/<<<<<<< markers in your editor.
git add
.git commit -m "Resolved merge conflicts" - To check for lost commits: Run the reflog command to see your repository's history of HEAD movements.
.git reflog
Diagnosing and Resolving Merge Conflicts
Merge conflicts occur when two or more developers modify the same lines in the same file since they last synchronized. Git pauses the process, requiring manual intervention.
Procedure:
- Identify Conflicts: When you run
git pullorgit merge, Git will output messages indicating conflicts and list the affected files. - Examine Markers: Open the conflicted file(s) in your text editor (like VS Code). You will see markers that look like this:
> feature/branch-name - Manual Resolution: Edit the file to contain exactly what you want (it could be your changes, the incoming changes, or a combination). Crucially, delete all conflict markers ().
- Stage and Commit: Once all files are clean of markers, stage them and complete the merge commit:
git addgit commit
Recovering Lost Commits using Git Reflog
The git reflog is your safety net. It records every time HEAD has moved—commits, merges, resets, etc.—allowing you to jump back to a previous state even if you force-pushed or executed an accidental reset.
Procedure:
- View History: Run
git reflog. This output shows commit hashes and messages associated with actions (e.g., HEAD@{5}: checkout: moving from main to develop). - Identify Target State: Look for the action that happened just before you lost your work. Note the full hash or the relative reference (e.g., HEAD@{3}).
- Reset/Checkout: To recover, use
git resetorgit checkoutwith the recorded reference. We recommendgit reset --hardif you want to completely overwrite your current branch state with the recovered point.git reset --hard HEAD@{N}Replace N with the number corresponding to the desired state from the reflog.
- Alternative (Soft Reset): If you want to keep the changes in your working directory but discard the commit history, use
git reset --soft HEAD@{N}.
Advanced Recovery: Cherry-Picking Lost Commits
If you only need one or two specific commits from a point in history (e.g., HEAD@{5}) without resetting your entire branch, git cherry-pick is the surgical tool you need.
Procedure:
- Find Target Hash: Use
git reflogto find the specific commit hash (SHA) of the change you want. - Execute Cherry-Pick: Run the cherry-pick command, targeting that exact SHA:
git cherry-pickThis applies only the changes from that specific commit onto your current branch tip.
- Handling Conflicts During Cherry-Pick: If the cherry-picked commit conflicts with your current work, Git will pause again. Follow the standard conflict resolution steps (Step 1) for this single change set, then
git add .andgit cherry-pick --continue.
Verification, Results & Preventative Maintenance
After resolving conflicts or recovering commits, verification is paramount. Never assume the fix worked without testing.
Verification Checklist:
- Check the file contents manually against what you remember before the conflict/loss.
- Run your application's test suite (e.g.,
npm testor equivalent). - Ensure the local branch history matches the expected state by comparing it to a remote source if possible.
Preventative Maintenance Tips:
- Always pull frequently (e.g.,
git pull --rebase) rather than waiting until the last minute. Rebase keeps your local history clean. - Never commit without running local tests first.
- Understand that
git reflogis highly valuable, but it only exists locally on your machine; always push important work regularly to a remote repository (GitHub/GitLab).
git add followed by git commit. Furthermore, you now know the powerful safety net of git reflog to recover lost commits using git reset --hard or target specific changes with git cherry-pick. Always back up critical work!Common Pitfalls & Advanced Troubleshooting
If standard remediation does not immediately resolve the symptoms for Mastering Git: How to Resolve Merge Conflicts and Recover Lost Commits using Reflog, review these advanced diagnostic contingencies on All Platforms:
- Peripheral & Bus Isolation: Detach non-essential USB accessories and external controllers to isolate IRQ and bus contention.
- Diagnostic Minimal Boot: Restart All Platforms in Safe / Diagnostic Mode to eliminate third-party daemon conflicts.
- Firmware Integrity: Check that hardware firmware and UEFI/BIOS revisions are updated to the manufacturer baseline.
git add <file-name>
git commit
git reflog
git reset --hard HEAD@{-N}
git cherry-pick <commit-sha-hash>
Frequently Asked Questions
What is the difference between git reset --hard and git revert?
<code>git reset --hard</code> rewrites history by moving HEAD back to an earlier state, effectively deleting subsequent commits locally. <code>git revert</code> creates a brand new commit that undoes the changes of a specific bad commit, preserving history integrity.
If I push my work and then realize I need to go back, can reflog help?
The standard <code>git reflog</code> only tracks local HEAD movements. If you have already pushed your "bad" state to a remote repository (like GitHub), the remote copy is safe, but recovering history requires force-pushing after resetting locally.
Do I always need to use --rebase when pulling?
Not always, but using <code>git pull --rebase</code> instead of a simple merge ensures that your local commits are placed on top of the remote changes, resulting in a cleaner, linear history and fewer accidental merge conflicts later.
Need help implementing this solution?
Our cybersecurity and infrastructure engineers can audit, harden, or configure your systems directly.
Community Discussion & Confirmations (0)
Technical questions, hardware findings, and verified fix reports.
No reader questions posted yet. Have you tried this solution or experienced an edge case? Post below.