skip to content
Ruban Selvarajah

I screwed up and now Git says my branch has diverged

/ 1 min read

git

No need to freak out. It happens to everyone at some point I think. Totally normal git experience.

Let’s 🚀

Picking up the pieces

1. Undo the faulty commits

luckily they both have not been pushed and yes, I committed twice before I noticed

Terminal window
git reset --soft HEAD~
git reset --soft HEAD~2

2. Make git stop tracking my changes

Terminal window
git restore --staged .

3. Verify that I’ve removed all my commits

Terminal window
git log

4. Stash my changes for now

Terminal window
git stash

5. Make git forget that I ever made those commits

Terminal window
git rebase

6. Get those pesky updates from upstream

Terminal window
git pull

7. Back to normal

Terminal window
git stash apply

Next time I’ll make sure to checkout into the correct branch before I start commiting. Or maybe I’ll come back to this post again…