10 votes

Git branched on its own, WTH happened?

Tags: ask.help, git

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.

11 comments

  1. [3]
    em-dash
    Link
    main is a normal branch. "Your branch" refers to the local branch called main, the one you're working on. I'd look at git log main vs git log origin/main (the latter is git's local cache of what's...

    main is a normal branch. "Your branch" refers to the local branch called main, the one you're working on.

    I'd look at git log main vs git log origin/main (the latter is git's local cache of what's on the remote main) and git diff main..origin/main to see where they differ.

    8 votes
    1. [2]
      Eric_the_Cerise
      Link Parent
      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.

      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.

      1. Ukcoder
        Link Parent
        I’ve often found that gitk —all is an exceptionally useful tool when trying to understand the state of a git repo

        I’ve often found that gitk —all is an exceptionally useful tool when trying to understand the state of a git repo

        1 vote
  2. [7]
    skybrian
    Link
    Did you amend a commit after you pushed it to the repo?

    Did you amend a commit after you pushed it to the repo?

    2 votes
    1. [5]
      pyeri
      Link Parent
      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" ?

      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" ?

      2 votes
      1. [4]
        em-dash
        Link Parent
        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...

        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.

        5 votes
        1. [3]
          pyeri
          Link Parent
          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. git push --force repository-name branch-name.

          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.

          git push --force repository-name branch-name.
          
          2 votes
          1. [2]
            em-dash
            Link Parent
            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...

            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.

            4 votes
            1. corney91
              Link Parent
              It's also safer to use --force-with-lease, just in case there were commits added since your last fetch: https://blog.developer.atlassian.com/force-with-lease/

              It's also safer to use --force-with-lease, just in case there were commits added since your last fetch: https://blog.developer.atlassian.com/force-with-lease/

              5 votes
    2. Eric_the_Cerise
      Link Parent
      No. Editing my original post now ... gimme a minute.

      No.

      Editing my original post now ... gimme a minute.

      1 vote
  3. NobleDusk
    Link
    You can try git reflog —all, if you did something that might give you a hint.

    You can try git reflog —all, if you did something that might give you a hint.

    2 votes