如何使用 git 修复本地过时错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5510113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:15:45  来源:igfitidea点击:

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

gitconflictgit-push

提问by vfclists

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 originshows 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.

git remote show origin显示master pushes to master (local out of date)。我很肯定它不会过时,因为我只从分支推送。

I have 2 questions.

我有2个问题。

  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?

  1. 是否可以强制本地分支覆盖远程?拉取将覆盖存储库中的内容之后的更改。

  2. 这是我第二次或第三次遇到这个问题。我唯一能想到的是 git 的本地版本是git version 1.7.3.1.msysgit.0(在 Windows 上),而远程版本是git version 1.6.5(Ubuntu Jaunty)。不同的 git 版本是否可能导致某些损坏?

回答by eckes

  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 pullfirst, 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 fetchinstead of git pulland have a look at the changes using git log origin/master...
  1. 见下面的更新)是的,这是可能的:git push --force。但是,只有在您绝对确定自上次以来没有人阅读过 repo 时才这样做push(请参阅如何将修改后的提交推送到远程 Git 存储库?深入讨论)。
  2. 对我来说,这似乎更像是你必须git pull先做,然后才能做到push。我不认为这是与时间相关的错误。该消息表明服务器上有较新的提交(由它们的提交哈希标识,而不是提交日期)...要查看更改的内容,请执行git fetch而不是git pull使用git log origin/master...查看更改

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

更新:同时,push学习了--force-with-lease确保您不会意外破坏某些内容的选项:https: //developer.atlassian.com/blog/2015/04/force-with-lease/--force只要有可能,就更喜欢这个!