git 更新被拒绝,因为您当前分支的提示落后

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

Updates were rejected because the tip of your current branch is behind

git

提问by Mike Christensen

I'm new to Git, so feel free to treat me like a newbie.

我是 Git 的新手,所以请把我当作新手。

Our workflow is such. We have a branch called devwhich I can reach at origin/dev. When we do changes, we create a branch off dev:

我们的工作流程就是这样。我们有一个分行dev,我可以在origin/dev. 当我们进行更改时,我们会在 dev 之外创建一个分支:

git checkout -b FixForBug origin/dev

git checkout -b FixForBug origin/dev

Now I have a branch called FixForBugwhich is tracking (I think that's the right word) origin/dev. Thus, if I do a git pullit'll bring in new changes from origin/devwhich is great. Now, when I'm finished with my fix, I push to a remote branch called the same thing.

现在我有一个名为FixForBugwhich is tracking (我认为这是正确的词)的分支origin/dev。因此,如果我这样做,git pull它会带来新的变化,origin/dev这是很棒的。现在,当我完成修复后,我推送到一个名为相同事物的远程分支。

First I pull down any changes from origin/devand do a rebase:

首先,我从以下位置拉下任何更改origin/dev并进行 rebase:

git pull --rebase

git pull --rebase

Then I push the changes to a remote branch of the same name:

然后我将更改推送到同名的远程分支:

git push origin FixForBug

git push origin FixForBug

Now, there's a branch on the remote server and I can create a pull request for that change to be approved and merged back in to the dev branch. I don't everpush anything to origin/devmyself. I'm guessing this is as pretty common workflow.

现在,远程服务器上有一个分支,我可以为要批准的更改创建拉取请求并将其合并回开发分支。我从不强迫origin/dev自己做任何事情。我猜这是非常常见的工作流程。

The first time I do a git push, it works fine and creates the remote branch. However, if I push a secondtime (let's say during code-review, someone points out a problem), I get the following error:

我第一次做 a 时git push,它工作正常并创建了远程分支。但是,如果我第二次推送(假设在代码期间,有人指出了一个问题),我会收到以下错误:

error: failed to push some refs to 'https://github.limeade.info/Limeade/product.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

错误:无法将一些引用推送到“ https://github.limeade.info/Limeade/product.git”提示:更新被拒绝,因为您当前分支的提示落后于提示:其远程副本。在再次推送之前集成远程更改(例如提示:'git pull ...')。提示:有关详细信息,请参阅“git push --help”中的“关于快进的注意事项”。

However, if I do a git statusit says I'm ahead of origin/devby 1 commit (which makes sense) and if I follow the hint and run git pull, it says everything is up to date. I thinkthis is because I'm pushing to a different branch than my upstream branch. I can fix this issue by running:

但是,如果我执行 agit status它说我领先origin/dev1 次提交(这是有道理的),如果我按照提示运行git pull,它说一切都是最新的。我认为这是因为我要推送到与上游分支不同的分支。我可以通过运行来解决这个问题:

git push -f origin FixForBug

git push -f origin FixForBug

In that case, it'll push the changes to the remote branch, saying (forced update)and everything appearsto be good on the remote branch.

在这种情况下,它会将更改推送到远程分支,说(强制更新)并且远程分支上的一切似乎都很好。

My Questions:

我的问题:

Why is -frequired in this scenario? Usually when you're forcingsomething, it's because you were doing something wrong or at least against standard practice. Am I ok doing this, or will it mess up something in the remote branch or create a hassle for whoever has to eventually merge my stuff into dev?

为什么-f在这种情况下需要?通常当你强迫某事时,那是因为你做错了什么,或者至少违反了标准做法。我这样做可以吗,还是会弄乱远程分支中的某些东西,或者给最终必须将我的东西合并到开发中的人带来麻烦?

回答by Keif Kraken

The -fisactually required because of the rebase. Whenever you do a rebase you would need to do a force push because the remote branch cannot be fast-forwarded to your commit. You'd alwayswant to make sure that you do a pull before pushing, but if you don't like to force push to master or dev for that matter, you can create a new branch to push to and then merge or make a PR.

-f实际需要,因为底垫。每当您进行 rebase 时,您都需要进行强制推送,因为远程分支无法快速转发到您的提交。您总是希望确保在推送之前执行拉取操作,但是如果您不喜欢为此强制推送到 master 或 dev,您可以创建一个新分支来推送,然后合并或创建 PR .

回答by mhatch

To make sure your local branch FixForBug is not ahead of the remote branch FixForBug pull and merge the changes before pushing.

确保您的本地分支 FixForBug 不领先于远程分支 FixForBug 在推送之前拉取并合并更改。

git pull origin FixForBug
git push origin FixForBug

回答by Greg Hewgill

If you want to avoid having to use -f, then you can use just

如果你想避免使用-f,那么你可以只使用

git pull

instead of

代替

git pull --rebase

The non-rebase will fetch the changes from origin/devand mergethem into your FixForBugbranch. Then, you will be able to run

非底垫将获取从变化origin/dev合并他们进入你的FixForBug分支。然后,您将能够运行

git push origin FixForBug

without using -f.

不使用-f.

回答by Allan F

The command I used with Azure DevOps when I encountered the message "updates were rejected because the tip of your current branch is behind" was/is this command:

当我遇到消息“更新被拒绝,因为当前分支的提示落后”时,我在 Azure DevOps 中使用的命令是/是以下命令:

git pull origin master

git pull 原点大师

(or can start with a new folder and do a Clone) ..

(或者可以从一个新文件夹开始并进行克隆)..

This answer doesn't address the question posed, specifically, Keif has answered this above, but it does answer the question's title/heading text and this will be a common question for Azure DevOps users.

这个答案没有解决提出的问题,具体来说,Keif 已经在上面回答了这个问题,但它确实回答了问题的标题/标题文本,这将是 Azure DevOps 用户的常见问题。

I noted comment: "You'd always want to make sure that you do a pull before pushing" in answer from Keif above !

我注意到评论:“你总是想确保你在推动之前做一个拉动”,上面来自 Keif 的回答!

I have also used Git Gui tool in addition to Git command line tool.

除了 Git 命令行工具之外,我还使用了 Git Gui 工具。

(I wasn't sure how to do the equivalent of the command line command "git pull origin master" within Git Gui so I'm back to command line to do this).

(我不确定如何在 Git Gui 中执行相当于命令行命令“git pull origin master”的操作,所以我回到命令行来执行此操作)。

A diagram that shows various git commands for various actions that you might want to undertake is this one:

显示您可能想要执行的各种操作的各种 git 命令的图表是这样的:

enter image description here

在此处输入图片说明

回答by Honey

This just happened to me.

这只是发生在我身上。

  • I made a pull request to our master yesterday.
  • My colleague was reviewing it today and saw that it was out of sync with our master branch, so with the intention of helping me, he merge master against my branch.
  • I didn't know he did that.
  • Then I merged master locally, tried to push it, but it failed. Why? Because my colleague merge with master created an extra commit I did not have locally!
  • 我昨天向我们的主人提出了拉取请求。
  • 今天同事在审核,发现和我们的master分支不同步,所以本着帮我的心,把master合并到我的分支上。
  • 我不知道他这样做了。
  • 然后我在本地合并了master,试图推送它,但它失败了。为什么?因为我的同事与 master 合并创建了一个额外的提交,我在本地没有

Solution: Pulldown my own branch so I get that extra commit. Then pushit back to my remote branch.

解决方案:拉下我自己的分支,以便获得额外的提交。然后回我的远程分支。

literally what I did on my branchwas:

从字面上看,我在我的分支上所做的是:

git pull
git push

回答by kris

It must be because of commit is ahead of your current push.

这一定是因为提交是在您当前的推送之前。

1) git pull origin "name of branch you want to push"

1) git pull origin "你要推送的分支名称"

2) git rebase

2) git rebase

if git rebase is successful , then good . Otherwise, you have resolve all merge conflicts locally and keep it continuing until rebase with remote is successful.

如果 git rebase 成功,那么很好。否则,您已经在本地解决了所有合并冲突并保持它继续进行,直到与远程重新绑定成功。

3)git rebase --continue

3)git rebase --continue

回答by Deepesh Nair

This is how I solved my problem

这就是我解决我的问题的方法

Let's assume the upstream branch is the one that you forked from and origin is your repo and you want to send an MR/PR to the upstream branch.

让我们假设上游分支是您分叉的分支,源是您的仓库,并且您想向上游分支发送 MR/PR。

You already have let's say about 4 commits and you are getting Updates were rejected because the tip of your current branch is behind.

你已经有让我们说 4 次提交,你正在得到 Updates were rejected because the tip of your current branch is behind.

Here is what I did

这是我所做的

First, squash all your 4 commits

首先,压缩所有 4 次提交

git rebase -i HEAD~4

You'll get a list of commits with pickwritten on them. (opened in an editor)

你会得到一个提交列表,上面pick写着。(在编辑器中打开)

example

例子

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3
pick c011a77 commit 4

to

pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3
squash c011a77 commit 4

After that, you can save your combined commit

之后,您可以保存合并的提交

Next

下一个

You'll need to stash your commit

你需要隐藏你的提交

Here's how

就是这样

git reset --soft HEAD~1
git stash

now rebase with your upstream branch

现在用你的上游分支变基

git fetch upstream beta && git rebase upstream/beta

Now pop your stashed commit

现在弹出你隐藏的提交

git stash pop

commit these changes and push them

提交这些更改并推送它们

git add -A
git commit -m "[foo] - foobar commit"
git push origin fix/#123 -f

回答by HoloLady

I had this issue when trying to push after a rebase through Visual Studio Code, my issue was solved by just copying the command from the git output window and executing it from the terminal window in Visual Studio Code.

我在尝试通过 Visual Studio Code 在 rebase 后推送时遇到了这个问题,我的问题是通过从 git 输出窗口复制命令并从 Visual Studio Code 的终端窗口执行来解决的。

In my case the command was something like:

在我的情况下,命令是这样的:

git push origin NameOfMyBranch:NameOfMyBranch

git push origin NameOfMyBranch:NameOfMyBranch