Search code examples
gitgit-reset

Git: I can't seem to reset my local main to origin/main


I want to do something very simple: update my local main to the remote one. Except it seems HEAD and origin/main are somehow pointing to an old commit. I want to fast forward HEAD and can only seem to find posts about rewinding.

On branch main, when I do git reset --hard HEAD or git reset --hard origin/main it says "HEAD is now at ...OLD COMMIT". If I do git status it says all is well, nothing to do. If I do git pull origin main it does in fact pull to the true most recent commit. BUT, if I now do git status again it says I am 10 commits ahead of main and should push.

I am so confused. What happened? How do I get back a local main that is up to date with the remote main branch?

For context, which I'm not sure is relevant here since I would like a hard reset: I originally had some local changes in main (which was out of date), so I did git checkout -b new_branch then committed all those changes to this new branch (to not lose them), then switched back to main. I then pulled from origin and tried to merge "new_branch" into it. I realized this is the reverse of what I wanted, I should have merged the updated main into new_branch. I basically now want to undo the merge, so I am trying to reset main...


Solution

  • The easiest way to do what you want is to first git fetch. Then do git reset --hard origin/main. To understand how this works, you should read about remote tracking branches. In short, this is a local copy of the remote branch. This remote tracking branch is updated each time you do git fetch. If someone else pushes to the branch, your local copy of origin/main (the remote tracking branch) won't be up to date until you git fetch.