Recover your lost commits on GitHub
Working within a team / collaborative environment, You'll have to be ready for the worst case scenario.
Hello,
I'm Vikash Kumar!
Scenario
You've pushed changes on GitHub recently.
Then another guy pushed his changes with -f flag.
Result, Your changes are vanished out from the history itself on GitHub. 😱
Don't panic
Follow these steps instead:
Don't pull latest changes from
mainormasterbranch.Check git logs.
If the log history shows your commit then you're on safety net.
Copy those commit ids for recovery steps.
git log
- Create a recovery branch and view logs here too
git checkout -b recover-commits
git log
# example commit
haxs56hvhh1jb38xh
ytwkjb1hv43jvbhv4
- Pick commits here
git cherry-pick haxs56hvhh1jb38xh
git cherry-pick ytwkjb1hv43jvbhv4
- Push this recovery branch
git push origin recover-commits
Go to GitHub raise the PR ( wait, if any build pipelines are ongoing ) Merge the branch: stash merge
Switch back to
mainbranch
git checkout main
git pull origin main
You've recovered your lost commits!
Thanks for Reading
