-1

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...

  • 1. Try doing `git fetch` before `git reset --hard origin/main`. 2. If you still need help, show us the output of `git log --oneline --graph --all`. Feel free to redact any sensitive information from this output. – Code-Apprentice May 03 '23 at 23:26
  • After reading your description more closely, it sounds like you made some commits on your local `main` that are not on the remote branch. Do you want to throw those away? – Code-Apprentice May 03 '23 at 23:27
  • Amazing, thank you @Code-Apprentice. I just needed to fetch. For more context, when I did git log origin/main it pointed to an old commit on remote. I am still slightly confused, because I thought things like "git pull" should also fetch first... – Amnon Attali May 03 '23 at 23:31
  • 1
    I'm not sure exactly what went wrong with it in this particular case, but it's amazing how rarely `git pull` is the right answer, especially in the modern world of merge requests. – Davis Herring May 03 '23 at 23:33
  • 1
    Yes, `git pull` will do a `git fetch`. And depending on which version of `git` you are using, it may do a `git merge` afterwards. Newer versions of `git` print out an error message instead and ask you to explicitly configure what behavior you want. – Code-Apprentice May 03 '23 at 23:35
  • With that said, I recommend you read about the different kinds of branches: local, remote, and remote-tracking. – Code-Apprentice May 03 '23 at 23:35
  • I am genuinely git ignorant, so thank you. If git pull fetches first, and I did pull, why did git fetch fix the problem? – Amnon Attali May 03 '23 at 23:37

4 Answers4

1

Q: I want to do something very simple: update my local main to the remote one.

A: There are several different facets to your question. There are also several different approaches, each with it's own pros/cons.

SUGGESTION:

  1. Clone your remote into a NEW, SEPARATE project directory
  2. Voila! You're done :)

ONE ALTERNATIVE:

  1. List your remote branches:

    git branch -r

  2. List your local branches:

    git branch -a

  3. Checkout the branch you want to "replace" (e.g. your local "master"):

    git checkout THE-BRANCH-I-WANT

  4. EXAMPLE: Assuming your local is named "master", and the upstream branch is "origin/master",

    git reset --hard origin/master

PS:

One benefit of the first suggestion (clone into a new project directory) is that you don't risk "making anything worse" in your local project. You can always go back to your current state (or any previous state) of any local branch (in the old, original project directory) at will...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

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.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

You can delete your local branch:

git branch -D main

then I'd suggest you fetch and checkout the remote branch:

git fetch
git checkout main

Note that this will delete any unmerged commits you may have on your local main branch.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
0

You could delete and recreate your local main:

  1. git checkout some_branch
  2. git branch -D main
  3. git fetch/pull
  4. git checkout main

After this your local main should be what the remote main is.

tymtam
  • 31,798
  • 8
  • 86
  • 126