DEV Community

Bhavani Ravi
Bhavani Ravi

Posted on • Originally published at bhavaniravi.com on

How I Got Out of a Knee Deep Git Problem

How I Got Out of a Knee Deep Git Problem

Problem

  • I am not used to rebasing or squashing commits, my new project strictly follows this hence I was pushed to do this.
  • I started with 4 commits which I had to squash
  • git rebase -i HEAD~4 and squashed all the commits all were good
  • But, as I pushed the code there was a conflict with the master
  • If you remember you cannot merge hence I did a git rebase -i master
  • Now I ended up having 10 more commits from god knows where
  • I cannot push all of them, hence I squashed them all again, in spite of the commits not being mine
  • I pushed my code, but the conflict was still there, at the brink of frustration I fixed the conflict in Github and left it there
  • As expected, I was asked not to merge from master and revert it
  • Now I am knee stuck with a merge, 9 commits squashed within which another 4 commits squashed

Solution - Git Reflog to the rescue

  • git reflog has a history of all the commands you ever executed
  • On running it I found the first squash I made
  • git reset --hard <commit_id> took me to that commit
  • One mistake I did last time when I rebased master was I didn't rebase from upstream so this time I ran git pull upstream master --rebase
  • Now I did a git add . to add my changes to rebase
  • git rebase --continue finishes the rebase successfully
  • Finally git push origin --force <branch-name> to push the final version with no-conflicts and just my commits squashed into one.

Top comments (2)

Collapse
 
stevetaylor profile image
Steve Taylor • Edited

Squashing via git rebase is hard. Do it in easy mode instead:

git fetch
git merge origin/master

If there are conflicts, resolve them and git commit to complete the merge. Then:

git reset --soft origin/master
git commit -m "(your message)"
git push --force
Collapse
 
mensdarko profile image
Darko Mens

Good to note