Skip to main content

Command Palette

Search for a command to run...

Recover your lost commits on GitHub

Updated
•1 min read•View as Markdown
V
Co-founder & Building @Quizforces | Web Developer

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:

  1. Don't pull latest changes from main or master branch.

  2. 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
  1. Create a recovery branch and view logs here too
git checkout -b recover-commits

git log
# example commit
haxs56hvhh1jb38xh

ytwkjb1hv43jvbhv4
  1. Pick commits here
git cherry-pick haxs56hvhh1jb38xh

git cherry-pick ytwkjb1hv43jvbhv4
  1. Push this recovery branch
git push origin recover-commits
  1. Go to GitHub raise the PR ( wait, if any build pipelines are ongoing ) Merge the branch: stash merge

  2. Switch back to main branch

git checkout main

git pull origin main

You've recovered your lost commits!

Thanks for Reading

GitHub Universe

Part 2 of 2

In this series, I am going to share my real development tricks to work arround GitHub environment

Start from the beginning

Shallow merging on GitHub

How to merge specific changes into feature branch interactively