gitgithubatlassian-sourcetree

How to fix detached origin/main in git and github


I am fairly new to Git and Github and have been working on a programming project, committing and pushing changes to Github. I am working on a branch named "Branch_ndimage.grey_closing"

After learning new ways and tools, I found that my origin/main and origin/head are still in the initial commit which is essentially empty. This is how the graph looks in SourcTree:

SourceTree graph

I tried to merge the latest commit to the first, but got the error "fatal: refusing to merge unrelated histories" which I understand.

I tried to rebase the first commit after the initial, and also the lastest commit, but although I get the local HEAD upmost on the graph, the origin/main and origin/head still point at the initial commit and are unconnected. Do I need to PUSH it to Github?

How do I get the latest commit of my branch to be the MAIN branch again and to have the HEAD point there also in the Github repository? I am not sure and I am afraid I will mess up the Github repo beyond repair if I experiment. I can work both with command line Git and SourceTree to fix this.


Solution

  • Firstly, explanation of what some of those labels mean:

    So, looking at that screenshot, we can see that:

    The result is that the "main" branch doesn't actually have anything to do with your history, and probably isn't useful to you. To get rid of it, log into Github and:

    1. Switch the UI to the "main" branch, and view the files there. Make sure there's nothing you want to keep.
    2. Change the default branch on the repository to "master" in the "Settings" of your repository. See this GitHub help pages for detailed instructions.
    3. Delete the "main" branch, by viewing the list of branches in GitHub, and clicking the wastebin icon. Detailed instructions for deleting are here.

    Then run git fetch --prune origin on your local copy, to remove the cached copy of the deleted branch.