为什么 Git 要求我在推送之前先拉取?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21223464/
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 09:36:29  来源:igfitidea点击:

Why does Git require me to pull before I push?

gitversion-controlgithubgit-push

提问by Zack

I keep getting an error saying: rejected master-> master (fetch first), failed to push some refs.... because remote contains work you do not have locally.

我不断收到一条错误消息:拒绝主 -> 主(先获取),未能推送一些参考......因为远程包含您在本地没有的工作。

I just want git to overwrite the files currently in the repository with the new uploads so I've been trying to use git push -u origin master, but this error keeps popping up. I'm brand new to git/github. Why is this happening?

我只是想让 git 用新上传的文件覆盖当前存储库中的文件,所以我一直在尝试使用 git push -u origin master,但是这个错误不断弹出。我是 git/github 的新手。为什么会这样?

I've tried to merge the existing files in the repo with the files on my desktop, but I keep getting merge conflicts. Not sure how to deal with these.

我尝试将 repo 中的现有文件与桌面上的文件合并,但我不断遇到合并冲突。不知道如何处理这些。

采纳答案by archetipo

git telling you that you must first:

git 告诉你,你必须首先:

git fetch

and later

然后

git add /commit /push

fetch is similar to pull but, pull merge the data in files in your local branch , fetch update only the branch structure and id..

fetch 与 pull 类似,但是,pull 合并本地分支中文件中的数据,fetch 只更新分支结构和 id..

if fetch does not work, it means that someone else committed to changing, and now your version must be upgraded before being released

如果 fetch 不起作用,则表示有人承诺更改,现在您的版本必须升级才能发布

回答by Phil

You are asked to pull before you push, because someone pushed changes to the server, after your last pull, so our local copy and the current server copy are not in sync. Pulling will merge the remote copy with your local one, which brings them back into sync and allows you to push.

你被要求在你推送之前拉取,因为有人在你上次拉取之后将更改推送到服务器,所以我们的本地副本和当前服务器副本不同步。拉动会将远程副本与您的本地副本合并,从而使它们恢复同步并允许您推送。

The reason that you are not allowed to push straight away, is that this willneed merging and mightlead to conflicts which need manual resolving. To avoid merging errors and conflicts, merging must alwaysbe done on the client side, never on the server.

不允许您立即推送的原因是,这需要合并并可能导致需要手动解决的冲突。为避免合并错误和冲突,合并必须始终在客户端完成,而不能在服务器端完成。