git 为什么我会收到“错误:无法推送一些参考”?

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

Why do i get "error: failed to push some refs"?

git

提问by Spyros

I have a remote git repository and a local one that i work with. Whenever i do any changes locally, i push them to the remote. Then i sometime do a "git commit" on the remote one to store the changes on the remote files.

我有一个远程 git 存储库和一个我使用的本地存储库。每当我在本地进行任何更改时,我都会将它们推送到远程。然后我有时会在远程文件上执行“git commit”以将更改存储在远程文件上。

I do not edit the remote repository directly at all. I just commit the changes. And i'm a single developer, no one else works on that repos.

我根本不直接编辑远程存储库。我只是提交更改。而且我是一名单一的开发人员,没有其他人在该存储库上工作。

Why do i get an error that, from what i know, means that i have to pull first ?

为什么我会收到一个错误,据我所知,这意味着我必须先拉?

I don't want to pull because the remote repos files are outdated and it will lose my local changes. This is really annoying, why does this happen ? And how can i fix without having to pull or recreate the repository ? (as you can see, this is sort of like a subversion type of version control style here)

我不想拉,因为远程 repos 文件已经过时,它会丢失我的本地更改。这真的很烦人,为什么会发生这种情况?我该如何修复而不必拉或重新创建存储库?(如您所见,这有点像这里的版本控制风格的颠覆类型)

EDIT - The error :

编辑 - 错误:

To ssh://...
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'ssh://...'
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.

回答by Brian Campbell

What you should be doing is creating the remote repository as a bare repository. A bare repository is just the git repo, without a current checkout (that is, it is like just the contents of the .gitdir in a regular Git repo, so it contains objects and refs, but it doesn't have an index or working copy of the file hierarchy). If you try pushing to a non-bare repository, the working copy will get out of sync with what is committed, and cause the kinds of problems you are seeing here.

您应该做的是将远程存储库创建为裸存储库。裸仓库只是 git 仓库,没有当前的检出(也就是说,它就像是.git常规 Git仓库中目录的内容,所以它包含对象和引用,但它没有索引或工作副本文件层次结构)。如果您尝试推送到非裸存储库,工作副本将与提交的内容不同步,并导致您在此处看到的各种问题。

You can create a bare repository using git init --bare repo.git. Or you can clone an existing repository as a bare repo using git clone --bare original-repo new-repo.git.

您可以使用git init --bare repo.git. 或者,您可以使用git clone --bare original-repo new-repo.git.

If you want to have a checked out copy of the repository on your server, you will need to create a new, non-bare repo on the server, and then pull into that repo from the bare repo that you push to.

如果你想在你的服务器上有一个仓库的检出副本,你需要在服务器上创建一个新的非裸仓库,然后从你推送到的裸仓库拉入该仓库。

回答by yuyonghang

this always means that you didn't synchronize the remote repository with local repo,so first you should synchronize them by using command git pull as following:

这总是意味着您没有将远程存储库与本地存储库同步,因此首先您应该使用命令 git pull 同步它们,如下所示:

git checkout master    
git pull origin master 

after this process ,you will synchronize them ,and then you can push changes to remote repo by followings:

在此过程之后,您将同步它们,然后您可以通过以下方式将更改推送到远程仓库:

git add [filename/directory]  
git commit -m"input your message"   
git remote add origin https://github.com//yourname.git     
git push origin master

回答by marquescharlon

Here is another option.

这是另一种选择。

git reset --mixed origin/master
git add .
git commit -m "Your message"
git push origin master

回答by Satish

I faced similar problem, and the following command worked. git push --set-upstream origin master

我遇到了类似的问题,以下命令有效。git push --set-upstream origin master