Search code examples
gitgithubfast-forward

fast-forward error in Github


I'm new to git as part of an online course I'm doing.

Currently doing a tutorial, and when I am getting the following error message when pushing to master for the first time:

$ git push -u origin master
To https://github.com/-/myappsample.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/-/myappsample.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I tried a git pull, but that also does not work:

$ git pull https://github.com/-/myappsample.git master
From https://github.com/-/myappsample
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

This is a brand new Github account with a new/empty repo.

Any ideas on what went wrong and how to fix this?

thanks


Solution

  • The above problem happens when the remote repository has been updated and the local one tries to push its new changes to remote without updating its content.

    The problem can be resolved with a pull. You can try any one or all of the following:

    1. git pull --all
    2. git pull origin < your_branch_name >
    3. git pull --rebase

    The last option is the best one as it tries to rebase your repo against the remote preventing an extra merge commit.

    A good example can be found here github resolve fast-forward errors. In case, after the pull, you get errors/conflicts, you need to resolve them also.