githubcommitbranching-and-merging

Can't merge 2 branches in github: There isn't anything to compare


I created a repository with the main branch and when I was about to push the code I accidentally created another branch - master. Now, when I try to merge the 2 branches I get the message:

Github merge

I also get the prompt:

commit history

Also, when i try to git push into the main directory I get the error:

! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/ATFUSA/ATFUSA.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Solution

  • Try:

    git pull
    git checkout main
    git merge master --allow-unrelated-histories
    git push <upstream> main -f
    

    Obviously, replace <upstream> with your repo.

    The key here is --allow-unrelated-histories. By default, git doesn't allow you to merge two completely different branches to prevent mistakenly merging two different projects, but this will override that protection.

    The -f in the push command makes it a force push that makes sure GitHub doesn't refuse the push. Typically, using -f is destructive, but here it's ok because main doesn't have any history right now- I just added it because I'm not sure the precise setup of your GitHub repo.

    See the docs for more info.