Recover lost changes after a bad commit with this step-by-step guide. Use git log, reflog, checkout, branch creation, cherry-picking, and merge/reset to restore your code.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Confirm the bad commit
git log
to locate the commit hash you want to recover from.
git log --oneline
Step 2: Inspect the reference log (reflog)
git reflog
to find the state just before your bad commit.
git reflog
Step 3: Check out the reference you want to restore
HEAD@{3}
) that represents the last good state.
git checkout HEAD@{3}
Step 4: Create a new branch from the good state
git checkout -b recover-from-bad-commit
Step 5: Compare and cherry-pick desired commits
git log
or a diff tool to compare between the bad branch and the new branch.
git cherry-pick
Step 6: Merge or reset your main branch
main
(or master
).
# Option A: merge
git checkout main
git merge recover-from-bad-commit
Option B: hard reset (destroys history after the reset point)
git checkout main
git reset --hard recover-from-bad-commit
Step 7: Push the corrections to GitHub
# Normal push
git push origin main
Force push (use with caution)
git push --force origin main
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.