I'm not a git expert, but I've run into this behavior and managed to get back into a working state, so here are my tips, all of which work using git version 2.28.0 (and likely earlier versions as well). I suspect someone who was more expert in git
could streamline this answer further.
This happens to me when I've done something to screw up my local master
/main
branch that causes it to be out of sync with the one it's supposed to be tracking. I haven't yet figured out what I've done to mess it up to begin with, as it hasn't happened frequently enough for me to diagnose the mistake in my behavior. [Update: I think I've now seen one behavior in my own workflow that causes this. I go to GitHub to view a PR and use the copy link from the "view command line instructions" information for how to get a copy of the PR in my workspace. The first of the two commands that are copied is git checkout -b ...
. However, sometimes, I inadvertently do this in a workspace that already had a branch with that name (typically, I've already tried an earlier draft of the PR), so the command fails, I'm still on my master/main branch, and then the next command gets pasted in and merges the branch into my master/main. Then it takes me awhile to realize that things got messed up].
There are two ways I notice that I've gotten into this state:
- via the editor window popping up when I do
git pull origin master
(as noted in the OP)
- when a PR that I've pushed to GitHub shows commits of the form
Merge branch 'master' of https://github.com/[myOrg]/[myRepo] into master
Here's how I fix this when it occurs:
- First, I find the oldest merge commit with this message in my branch's history:
- browse the log:
git log
- search back in time for the oldest commit with the message
Merge branch 'master' of https://github.com/[myOrg]/[myRepo] into master
- look for the SHA of the commit just prior to that (i.e., the point where my
master
branch diverged from the one it's supposed to be tracking)
- Back my branch up to that SHA:
git checkout [SHA]
- this will put your branch into 'detached HEAD' state
- Rename my messed up master branch to get it out of the way
git branch -m master master-broken
(optionally, you could delete it, but this is safer, and you can always delete it later)
- Rename my current detached HEAD branch to master:
git checkout -b master
- Catch the branch back up to where it should be:
git pull [upstream source] master
(where, in the OP's case, I'd expect this to be git pull origin master
)
A key insight to understanding this fix is to realize that there's nothing deeply special about the master
branch—it's just a convention. So there's no problem with deleting it (or renaming it) and creating a new one with that name.
git commit
first?pull
s always just adding to what was before, or di youcommit
stuff meanwhile? Perhaps you should be doinggit pull --rebase
instead?git commit
first. @vonbrand - I'm not entirely sure what you're asking about thepull
s