Developer Tools & Environments INTERMEDIATE All Platforms ⏱️ 4 min read 📅 Updated: 23 Sep 2026

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.

Gurveer Singh
Gurveer Singh ✓ Verified Specialist
Founder & Chief Architect · hSECURITIES Labs
⚡ FAST-TRACK PROTOCOL

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
    .
🛠️ Tools & Prerequisites: Git installed and configured on your local machine. A terminal or Git Bash environment (works across Windows, macOS, and Linux). Basic understanding of version control concepts.
STEP 1

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:

  1. Identify Conflicts: When you run git pull or git merge, Git will output messages indicating conflicts and list the affected files.
  2. 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
  3. 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 ().
  4. Stage and Commit: Once all files are clean of markers, stage them and complete the merge commit:
    git add 
    git commit
STEP 2

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:

  1. 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).
  2. 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}).
  3. Reset/Checkout: To recover, use git reset or git checkout with the recorded reference. We recommend git reset --hard if 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.

  4. 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}.
STEP 3

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:

  1. Find Target Hash: Use git reflog to find the specific commit hash (SHA) of the change you want.
  2. Execute Cherry-Pick: Run the cherry-pick command, targeting that exact SHA:
    git cherry-pick 

    This applies only the changes from that specific commit onto your current branch tip.

  3. 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 . and git cherry-pick --continue.

FINAL STEP

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 test or 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 reflog is highly valuable, but it only exists locally on your machine; always push important work regularly to a remote repository (GitHub/GitLab).
Resolution Complete: You have successfully learned how to manually resolve merge conflicts by editing markers and using 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!
TROUBLESHOOTING

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.
COMMAND LINE / TERMINAL
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.

Engineering Assistance

Need help implementing this solution?

Our cybersecurity and infrastructure engineers can audit, harden, or configure your systems directly.

Request Technical Assistance →
Gurveer Singh
Gurveer Singh ✓ Verified Author
Founder & CEO at hSECURITIES | Cybersecurity Educator | Security Researcher | Ethical Hacking Enthusiast | Technology Mentor
Meet the Founder →

Gurveer Singh Saggu is the Founder and CEO of hSECURITIES, a cybersecurity and technology education platform focused on practical learning, digital security awareness, hardware diagnostics, Linux administration, and real-world cybersecurity training.

With a strong passion for technology, ethical hacking, and security education, he has spent years helping students, professionals, and organizations understand modern cyber threats and develop practical technical skills. Through hSECURITIES, he creates hands-on learning resources, technical guides, security awareness content, and community-driven initiatives that make cybersecurity more accessible.

His mission is to bridge the gap between theoretical knowledge and practical implementation by providing step-by-step learning experiences, technical troubleshooting resources, and security-focused educational platforms.

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.

Ask a Question or Share Solution