git push 被拒绝

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

git push rejected

gitpush

提问by Andriy Drozdyuk

I give up! Whenever I try to push I get a stupid:

我放弃!每当我尝试推动时,我都会变得愚蠢:

! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '[email protected]:companyX/projectX.git'

Our team has a new git setup. Instead of making private branches I now Forked our main repository (on github) to create my own copy.

我们的团队有一个新的 git 设置。我现在没有创建私有分支,而是将我们的主存储库(在 github 上)分叉来创建我自己的副本。

At some point what I did was:

在某些时候,我所做的是:

$ git fetch upstream master:upstreammaster

So here is my current setup::

所以这是我目前的设置::

$ git branch
master
* upstreammaster

$ git remote -v
origin  [email protected]:userX/projectX.git
upstream    [email protected]:companyX/projectX.git

where userX is my private repository.

其中 userX 是我的私人存储库。

So I go and make some changes to my upstreammaster branch, and the PULL from "upstream master". Everything merges and stuff:

所以我去对我的 upstreammaster 分支和“upstream master”的 PULL 做一些更改。一切都合并了:

$ git pull upstream master
remote: Counting objects: 95, done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 60 (delta 54), reused 0 (delta 0)
Unpacking objects: 100% (60/60), done.
From [email protected]:companyX/projectX
 * branch            master     -> FETCH_HEAD
Merge made by recursive.
stuff                      |  165 ++++++++++++--------
stuff                      |   35 ++--
stuff                       |  107 ++++++++++---
stuff                       |  105 ++++++++++---
stuff             |   24 ++--
stuff               |    9 +-
stuff                   |   53 +++----
stuff            |   44 +++---
stuff              |   52 +++----
stuff |   32 +----
stuff          |    4 +-
 stuff             |  138 ++++++++---------
stuff     |   58 ++++----
stuff    |  115 ++++++++------
stuff          |    5 +-
stuff                       |   39 ++---
stuff                        |   28 ++--
 17 files changed, 560 insertions(+), 453 deletions(-)

but then when I try to do:

但是当我尝试这样做时:

$ git push upstream master
To [email protected]:companyX/projectX.git
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '[email protected]:companyX/projectX.git'

Any help would be greately appreciated! If you need clarification please ask, I will reply!

任何帮助将不胜感激!如果您需要澄清请询问,我会回复!

采纳答案by Jarret Hardie

When doing a push, try specifying the refspec for the upstream master:

在进行推送时,尝试为上游主服务器指定 refspec:

git push upstream upstreammaster:master

回答by Pat Notz

Jarret Hardie is correct. Or, first merge your changes back into master and then try the push. By default, git pushpushes all branches that have names that match on the remote -- and no others. So those are your two choices -- either specify it explicitly like Jarret said or merge back to a common branch and then push.

贾瑞特·哈迪是对的。或者,首先将您的更改合并回 master,然后尝试推送。默认情况下,git push推送名称与远程匹配的所有分支——没有其他分支。所以这是你的两个选择——要么像 Jarret 说的那样明确指定它,要么合并回一个公共分支然后推送。

There's been talk about this on the Git mail list and it's clear that this behavior is not about to change anytime soon -- many developers rely on this behavior in their workflows.

在 Git 邮件列表上已经讨论过这个问题,很明显这种行为不会很快改变——许多开发人员在他们的工作流程中依赖这种行为。

Edit/Clarification

编辑/澄清

Assuming your upstreammasterbranch is ready to push then you could do this:

假设您的upstreammaster分支已准备好推送,那么您可以这样做:

  1. Pull in any changes from the upstream.

    $ git pull upstream master

  2. Switch to my local master branch

    $ git checkout master

  3. Merge changes in from upstreammaster

    $ git merge upstreammaster

  4. Push my changes up

    $ git push upstream

  1. 从上游引入任何更改。

    $ git pull 上游主

  2. 切换到我本地的 master 分支

    $ git checkout master

  3. 合并来自的更改 upstreammaster

    $ git merge upstreammaster

  4. 推送我的更改

    $ git push 上游

Another thing that you maywant to do before pushing is to rebaseyour changes against upstream/master so that your commits are all together. You can either do that as a separate step between #1 and #2 above (git rebase upstream/master) or you can do it as part of your pull (git pull --rebase upstream master)

在推送之前您可能想要做的另一件事是rebase针对上游/主服务器进行更改,以便您的提交全部在一起。您可以将其作为上述 #1 和 #2 之间的单独步骤git rebase upstream/master来执行( ),也可以作为 pull ( git pull --rebase upstream master) 的一部分来执行

回答by user2368055

First use

第一次使用

git pull https://github.com/username/repository master

and then try

然后尝试

git push -u origin master

回答by Rob Watson

First, attempt to pull from the same refspec that you are trying to push to.

首先,尝试从您尝试推送到的同一个 refspec 中提取。

If this does not work, you can force a git pushby using git push -f <repo> <refspec>, but use caution: this method can cause references to be deleted on the remote repository.

如果这不起作用,你可以强制git push使用git push -f <repo> <refspec>,但使用注意:此方法可能会导致引用要在远程存储库中删除。

回答by alekwisnia

Is your repository at "upstream" a bare repository? I got the same error, but when I change to bare they no longer happen.

您在“上游”的存储库是裸存储库吗?我遇到了同样的错误,但是当我更改为裸机时,它们不再发生。

回答by Rahul Bali

If nothing works, try:

如果没有任何效果,请尝试:

git pull --allow-unrelated-histories <repo> <branch>

then do:

然后做:

git push --set-upstream origin master

回答by Ashif

If push request is shows Rejected, then try first pull from your github account and then try push.

如果推送请求显示已拒绝,请先尝试从您的 github 帐户中拉取,然后再尝试推送。

Ex:

前任:

In my case it was giving an error-

就我而言,它给出了一个错误-

 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ashif8984/git-github.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

****So what I did was-****

****所以我做的是-****

$ git pull
$ git push

And the code was pushed successfully into my Github Account.

并且代码成功推送到我的 Github 帐户中。