Reputation: 352
I have created a Git repository on GitHub, and a local repository too. First I pulled the remote repository into the local one. Then I added a file, staged the file, committed it, and now I try to do a push to the remote repository again, but it fails with this message:
Pushing to https://github.com/jjenkov/java-utils To https://github.com/jjenkov/java-utils ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/jjenkov/java-utils' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I am completely new to Git and Github, so maybe I am doing something wrong. I have “fetched” the remote repository into the local one, although the remote repository should be empty (except for a readme file generated by GitHub).
Does anyone know what the problem is, or where I can read a solution?
Upvotes: 0
Views: 461
Reputation: 139411
A non-fast-forward push means the remote master
is not an ancestor of your local master
. Git is refusing the push as a safety mechanism to prevent destroying work.
The output of git log --graph --decorate --pretty=oneline --abbrev-commit --all
will give you a graphical depiction of your repository’s history. If you are sure you don’t mind replacing or destroying what is in GitHub, run
$ git push --force origin master
The --force
option is documented as (with added emphasis)
-f
,--force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it. This flag disables the check. This can cause the remote repository to lose commits; use it with care.
To keep what is there, you will need to rebase your local master on top of the one on GitHub and then push the result.
$ git fetch
$ git rebase origin/master master
$ git push origin master
or just
$ git checkout master
$ git pull --rebase
The sequence above is the sunny-day case. You may need to resolve conflicts, but that seems unlikely given your description.
Upvotes: 0
Reputation: 363487
The remote repo is not empty; it contains a README
that you probably created through the GitHub web interface. You have to pull it before you can push to it:
git pull --rebase origin master
git push
(The --rebase
is not strictly necessary but avoids an ugly merge commit.)
Upvotes: 2