git:更新被拒绝,因为远程包含您本地没有的工作

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

git: updates were rejected because the remote contains work that you do not have locally

gitversion-controlmergecommitbitbucket

提问by delos

I'm working on a team with a few developers using git on BitBucket. We are all working on a devbranch, not pushing to masteruntil a release.

我正在与几个使用 BitBucket 上的 git 的开发人员一起工作。我们都在一个dev分支上工作,master直到发布才推送。

One of the developers committed incorrect code that overwrote my own by accident, and now I am trying to push the correct code back to the repo. I have been reading on this error for a few days now, I can't push to the repo anymore because I am getting the following error:

其中一位开发人员提交了错误的代码,这些代码不小心覆盖了我自己的代码,现在我正试图将正确的代码推回 repo。我已经阅读这个错误几天了,我不能再推送到 repo,因为我收到以下错误:

 ! [rejected]        master -> dev (fetch first)
error: failed to push some refs to 'https://[email protected]/repo_user/repo_name.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.

I follow the instructions and pull, but then I receive a merge conflict. After entering a message for the merge conflict, my local code is now the incorrect code that the other developer uploaded by accident (as expected from the pull). So I replace the incorrect code with the backup I copied before commiting, and when I try to push again, I get the same error.

我按照说明和pull,但随后我收到了合并冲突。输入合并冲突的消息后,我的本地代码现在是其他开发人员意外上传的错误代码(正如预期的那样pull)。所以我用提交前复制的备份替换了不正确的代码,当我再次尝试推送时,我得到了同样的错误。

It is really frustrating, I really want to help out my team and contribute, but I can't because of this error. Does anyone know how to solve this issue? I would very much appreciate any help.

真的很沮丧,我真的很想帮助我的团队并做出贡献,但我不能因为这个错误。有谁知道如何解决这个问题?我将非常感谢任何帮助。

These are the commands I run in order to commit, if it helps anyone out:

这些是我为了提交而运行的命令,如果它可以帮助任何人:

git pull remotename master:dev
git add --all
git commit -m "some message"
git pull remotename master:dev
git push remotename master:dev

I would have thought that if I kept this order, I would not receive merge conflicts. I guess I was wrong. Thanks again

我原以为如果我保持这个顺序,我就不会收到合并冲突。我想我错了。再次感谢

Update: I should add that I have looked for a few hours on Google and stackoverflow, and followed different instructions, but I still can't pushto the devbranch.

更新:我应该补充一点,我已经在 Google 和 stackoverflow 上查找了几个小时,并遵循了不同的说明,但我仍然无法push到达dev分支。

采纳答案by Jeff

git pull <remote> master:devwill fetch the remote/masterbranch and merge it into your local/devbranch.

git pull <remote> master:dev将获取remote/master分支并将其合并到您的local/dev分支中。

git pull <remote> devwill fetch the remote/devbranch, and merge it into your current branch.

git pull <remote> dev将获取remote/dev分支,并将其合并到您当前的分支中。

I think you said the conflicting commit is on remote/dev, so that is the branch you probably intended to fetch and merge.

我想你说过冲突提交是 on remote/dev,所以这就是你可能打算获取和合并的分支。

In that case, you weren't actually merging the conflict into your local branch, which is sort of weird since you said you saw the incorrect code in your working copy. You might want to check what is going on in remote/master.

在这种情况下,您实际上并没有将冲突合并到您的本地分支中,这有点奇怪,因为您说您在工作副本中看到了不正确的代码。您可能想检查remote/master.

回答by Donal

Use this command in termial

临时使用此命令

git push -f origin master

git push -f 源主

回答by Himanshu

It happens when we are trying to push to remote repository but has created a new file on remote which has not been pulled yet, let say Readme. In that case as the error says

当我们尝试推送到远程存储库但在远程创建了一个尚未拉取的新文件时会发生这种情况,比如Readme. 在这种情况下,正如错误所说

git rejects the update

git 拒绝更新

as we have not taken updated remote in our local environment. So Take pull first from remote

因为我们没有在本地环境中更新远程。所以先从远程拉取

git pull

It will update your local repository and add a new Readmefile. Then Push updated changes to remote

它将更新您的本地存储库并添加一个新Readme文件。然后将更新的更改推送到远程

git push origin master

回答by Samir Poudel

Force to push

用力推

git push -f origin master

git push -f 源主

回答by Sravya

This usually happens when the repo contains some items that are not there locally. So in order to push our changes, in this case we need to integrate the remote changes and then push.

这通常发生在 repo 包含一些本地不存在的项目时。所以为了推送我们的更改,在这种情况下我们需要集成远程更改然后推送。

So create a pull from remote

所以创建一个从远程拉

git pull origin master

Then push changes to that remote

然后将更改推送到该遥控器

git push origin master

回答by delos

I fixed it, I'm not exactly sure what I did. I tried simply pushing and pulling using:

我修复了它,我不确定我做了什么。我尝试简单地推拉使用:

git pull <remote> devinstead of git pull <remote> master:dev

git pull <remote> dev代替 git pull <remote> master:dev

Hope this helps out someone if they are having the same issue.

希望这对遇到相同问题的人有所帮助。

回答by d337

Well actually github is much simpler than we think and absolutely it happens whenever we try to pusheven after we explicitly inserted some files in our git repository so, in order to fix the issue simply try..

好吧,实际上 github 比我们想象的要简单得多,而且即使我们在 git 存储库中明确插入了一些文件之后,只要我们尝试推送,它就会发生,所以,为了解决这个问题,只需尝试..

: git pull

: git 拉

and then..

进而..

: git push

: git push

Note: if you accidently stuck in vim editor after pulling your repository than don't worry just close vim editor and try push :)

注意:如果您在拉取存储库后不小心卡在 vim 编辑器中,请不要担心,只需关闭 vim 编辑器并尝试 push :)

回答by Eduardo Ramos

You can try this: git pull origin master --rebase

你可以试试这个: git pull origin master --rebase

回答by Prabhat

I have done below steps. finally it's working fine.

我已经完成了以下步骤。最后它工作正常。

Steps

脚步

1) git init

1) git 初始化

2) git status (for checking status)

2) git status (用于检查状态)

3) git add . (add all the change file (.))

3) git 添加。(添加所有更改文件 (.))

4) git commit -m "<pass your comment>"

4) git commit -m "<pass your comment>"

5) git remote add origin "<pass your project clone url>"

5)git远程添加原点 "<pass your project clone url>"

6) git pull --allow-unrelated-histories "<pass your project clone url>"master

6) git pull --allow-unrelated-history "<pass your project clone url>"master

7) git push -u "<pass your project clone url>"master

7) git push -u "<pass your project clone url>"master

回答by gaoyehua

You need to input:

您需要输入:

$ git pull
$ git fetch 
$ git merge

If you use a git push origin master --force, you will have a big problem.

如果你使用 a git push origin master --force,你会遇到一个大问题。