If you’ve ever worked on a large project, chances are you’ve encountered a pull request (PR) disaster. Here's how I royally messed up a PR—and how Git commands saved my ass.
The Disaster
I started by branching out changes from two unmerged PRs, creating a new "combined" branch (PR 3) to merge the changes from PR 1 and PR 2.
Everything seemed fine until I noticed PR 3 was st
ill showing changes from one of the unmerged PRs. Thinking a rebase from PR 2 would fix it, I went ahead confidently.
Spoiler: I was wrong.
The rebase changed the base branch of PR 3 to PR 2, exposing all changes from PR 1.
Trying to fix this, I thought I could simply change the base branch on GitHub.
Salt in the wound: I selected the wrong branch in GitHub’s dropdown.
actual:
long-description-combine-a-deploy
long-description-combine-a
irl:
long-description-combine-..
long-description-combine-..
GitHub instantly closed my PR because the content didn’t match.
Thankfully, I had PR 3 branch stored in another backup repo. I forced-pushed a new combine PR (PR 1 and PR 2) to PR 3, thinking I could reused my branch. But when I double-checked, the backup branch was missing my latest commit.
I’d lost my work, and my commit was nowhere to be found. (screaming into a pillow)
The Recovery Plan
Here’s how I find the missing commit hash using git reflog:
git reflog
...
c3d4f5b (HEAD -> feature) HEAD@{0}: commit: My precious changes
a1b2c3d HEAD@{1}: part of pr3 commit
9e8f7a6 HEAD@{2}: part of pr3 commit
...
git reset HEAD@{0}
git reflog
Git keeps track of updates to the tip of branches using a mechanism called reference logs, or "reflogs." git reflog
is a Git command that tracks updates to your repository’s HEAD. It shows a history of all actions, like commits, rebases, resets, or checkouts—even if those changes aren’t part of the visible branch history.
Why is it useful?
If you lose a commit or accidentally reset your branch, git reflog helps you find and recover the missing work by providing the commit hashes of past actions.
All was well again.
Saviour article
https://medium.com/@krokowski.dx/how-to-undo-your-git-failure-b76e31ecac74
Top comments (0)