Git branched on its own, WTH happened?
I cannot push nor pull to/from my local repo to/from my remote. I get the message "Your branch and 'origin/main' have diverged", etc.
I will be able to fix it, I'm already in the process of refreshing my memory on how to rebase this cleanly.
But ... I am the only person working on this. I have only one local and one central/remote repository. And I never made a branch.
It's just me, working on one computer, right here ... make a few commits locally, then push 'em ... make a few commits locally, then push 'em. That is my entire workflow.
How in the hell did I end up with "my branch" (???) and the origin/main branch being in conflict with each other?
Any Git gurus care to take a stab at this?
Thanks in advance.
Edit, more info:
I am guessing here, but for awhile I was working in a VM on my local machine ... but Android SDK is too much of a resource hog, so I committed and pushed everything on the VM, shut it down, and then pulled latest down to my actual machine ... or at least, that's how I remember it.
My working theory is that, somehow, I screwed up that "switching between machines" process, although, so far, no clue as to how/why I did.
Theory #2 is that Android SDK did something sneaky/stupid behind the scenes -- my last several commits have been done through the Android Studio IDE, not directly in the terminal.
Last Edit:
D'oh!
Found it. I made a couple of "quick changes" to the project's README.md file directly in the remote forge, and that's what screwed me up.
main
is a normal branch. "Your branch" refers to the local branch calledmain
, the one you're working on.I'd look at
git log main
vsgit log origin/main
(the latter is git's local cache of what's on the remote main) andgit diff main..origin/main
to see where they differ.This was the most helpful comment for me, being able to easily see exactly where the two roads diverged in the wood, and where I had taken the one less traveled.
I’ve often found that
gitk —all
is an exceptionally useful tool when trying to understand the state of a git repoDid you amend a commit after you pushed it to the repo?
Not the OP but should it be an issue if one amends a commit? Does it fork another branch or create some sort of "diversion" ?
Yes. It replaces the commit, so if you push then amend, your remote branch has commits A->B->C but your local branch has A->B->D. It's kind of like abandoning the original branch and creating a new one with almost the same changes.
Got it. But there has to be a way to inform remote of these local amends using git reset or something?
Edit
You can use
git push --force
it seems.Yep,
push --force
. It's considered terribly impolite to do on a public repository (since it'll cause this same issue for everyone else, because now their local copy is diverged from what's there now), but if you're working on a project by yourself, it's fine.It's also safer to use
--force-with-lease
, just in case there were commits added since your lastfetch
: https://blog.developer.atlassian.com/force-with-lease/No.
Editing my original post now ... gimme a minute.
You can try git reflog —all, if you did something that might give you a hint.