gitvisual-studiogit-resetgit-revert

Git says my branch is ahead of origin - even after revert/reset


On a recently cloned project, I just now made an unintentional change/commit to my local main branch. (I had intended to create and check out a new feature branch for the change, but I wasn't thinking :-)).

Upon realizing my mistake, I reverted the change. While this did indeed set my code back to the state it was in before I changed it, running git status nonetheless indicated that my branch was a commit ahead of the origin (i.e. the hosted repo).

No problem, I thought: I'll just do a git reset --hard. Nope, that didn't work - my console still shows that I was ahead of origin. I also tried git reset --hard head. Still no luck. And multiple times during this process I ran git pull origin main, to no effect.

My question is: how to I rid my git history of these commits, so that git status shows me as being completely in sync with origin?


Solution

  • The command git reset --hard HEAD would just reassign HEAD to itself and delete any staged or unstaged changes.

    You want to use the command

    git reset --hard HEAD~2
    

    The argument HEAD~2 shifts your HEAD reference behind by two commits (the unwanted commit and the revert commit). Once done, a git status should show complete sync with the origin.