Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix a local out of date error with git?

I am trying to push to a remote repo but keep getting the error below.

$ git push
To [email protected]:/home/user/repos/remoterepo.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'user@remote:/home/user/repos/remoterepo.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

git remote show origin shows master pushes to master (local out of date). I am positive that it shouldn't be out of date as I push only from the branch.

I have 2 questions.

  1. is it possible to force the local branch to overwrite the remote? Pulling will overwrite the changes which are definitely later that the stuff in the repository.

  2. This is about the 2nd or 3rd time I have had this problem. The only thing I can think of that the local version of git is git version 1.7.3.1.msysgit.0 (on Windows)and the remote version is git version 1.6.5 (Ubuntu Jaunty). Is it possible that the different git versions may be causing some corruption?

like image 995
vfclists Avatar asked Dec 10 '22 09:12

vfclists


1 Answers

  1. (see update below) yes, it is possible: git push --force. But do that only if you are absolutely sure that nobody has read the repo since the last push (see How do I push amended commit to the remote Git repository? for an in-deep discussion).
  2. To me, it seems more like you have to git pull first, before you're able to push. I don't think that it is a time related error. The message indicates that there are newer commits on the server (identified by their commit hash, not the commit date)... To see, what's changed, do a git fetch instead of git pull and have a look at the changes using git log origin/master...

Update: in the meanwhile, push learned the --force-with-lease option that ensures that you're not accidently breaking something: https://developer.atlassian.com/blog/2015/04/force-with-lease/. Prefer this over --force whenever possible!

like image 60
eckes Avatar answered Dec 17 '22 18:12

eckes