[英]Updates were rejected because the tip of your current branch is behind its remote counterpart
Our workflow is such.我们的工作流程是这样的。 We have a branch called
dev
which I can reach at origin/dev
.我们有一个名为
dev
的分支,我可以在origin/dev
到达它。 When we do changes, we create a branch off dev:当我们进行更改时,我们会创建一个 dev 分支:
git checkout -b FixForBug origin/dev
Now I have a branch called FixForBug
which is tracking (I think that's the right word) origin/dev
.现在我有一个名为
FixForBug
的分支,它正在跟踪(我认为这是正确的词) origin/dev
。 Thus, if I do a git pull
it'll bring in new changes from origin/dev
which is great.因此,如果我执行
git pull
操作,它将带来来自origin/dev
的新更改,这很棒。 Now, when I'm finished with my fix, I push to a remote branch called the same thing.现在,当我完成修复后,我将推送到一个名为同一事物的远程分支。
First I pull down any changes from origin/dev
and do a rebase:首先,我从
origin/dev
中提取所有更改并进行 rebase:
git pull --rebase
Then I push the changes to a remote branch of the same name:然后我将更改推送到同名的远程分支:
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 ever push anything to
origin/dev
myself.我自己从不向
origin/dev
推送任何东西。 I'm guessing this is as pretty common workflow.我猜这是很常见的工作流程。
The first time I do a git push
, it works fine and creates the remote branch.我第一次执行
git push
时,它工作正常并创建了远程分支。 However, if I push a second time (let's say during code-review, someone points out a problem), I get the following error:但是,如果我第二次推送(比如说在代码审查期间,有人指出了一个问题),我会收到以下错误:
error: failed to push some refs to 'https://github.mydomain.info/Product/product.git'
错误:无法将某些引用推送到“https://github.mydomain.info/Product/product.git”
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart.提示:更新被拒绝,因为你当前分支的尖端落后于它的远程分支。 Integrate the remote changes (eg hint: 'git pull...') before pushing again.
在再次推送之前集成远程更改(例如提示:'git pull ...')。
See the 'Note about fast-forwards' in 'git push --help' for details.有关详细信息,请参阅“git push --help”中的“关于快进的注意事项”。
However, if I do a git status
it says I'm ahead of origin/dev
by 1 commit (which makes sense) and if I follow the hint and run git pull
, it says everything is up to date.但是,如果我执行
git status
,它会说我比origin/dev
领先 1 次提交(这是有道理的),如果我按照提示运行git pull
,它会说一切都是最新的。 I think this is because I'm pushing to a different branch than my upstream branch.我认为这是因为我正在推送到与我的上游分支不同的分支。 I can fix this issue by running:
我可以通过运行来解决这个问题:
git push -f origin FixForBug
In that case, it'll push the changes to the remote branch, saying (forced update) and everything appears to be good on the remote branch.在这种情况下,它会将更改推送到远程分支,说(强制更新)并且远程分支上的一切似乎都很好。
My Questions:我的问题:
Why is -f
required in this scenario?为什么在这种情况下需要
-f
? Usually when you're forcing something, 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?
我可以这样做吗,还是会弄乱远程分支中的某些东西,或者给最终必须将我的东西合并到开发人员中的人制造麻烦?
The -f
is actually required because of the rebase.由于变基,实际上需要
-f
。 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 always want 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.
您总是希望确保在推送之前进行拉取,但如果您不想为此强制推送到 master 或 dev,您可以创建一个新分支来推送,然后合并或进行 PR .
*"The tip of your current branch is behind its remote counterpart"*
means that there have been changes on the remote branch that you don't have locally. *"The tip of your current branch is behind its remote counterpart"*
表示远程分支上发生了您在本地没有的更改。 And Git tells you to import new changes from REMOTE
and merge it with your code and then push
it to remote. Git 会告诉你从
REMOTE
导入新的更改并将其与你的代码合并,然后将其push
送到远程。
You can use this command to force changes to the server with the local repository ().您可以使用此命令强制更改具有本地存储库的服务器()。 remote repo code will be replaced with your local repo code.
远程仓库代码将替换为您的本地仓库代码。
git push -f origin master
With the -f
tag you will override the remote branch code with your local repo code.使用
-f
标签,您将使用本地 repo 代码覆盖远程分支代码。
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
Set the current branch name, like master :设置当前分支名称,如master :
git pull --rebase origin master
git push origin master
git pull --rebase origin master
git push origin master
Or branch name develop或分支名称develop
git pull --rebase origin develop
git push origin develop
git pull --rebase origin develop
git push origin develop
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/dev
and merge them into your FixForBug
branch.非变基将从
origin/dev
获取更改并将它们合并到您的FixForBug
分支中。 Then, you will be able to run然后,您将能够运行
git push origin FixForBug
without using -f
.不使用
-f
。
我们可以通过以下 cmd 使用本地存储库强制更改 GitHub:
git push -f origin main
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
(or can start with a new folder and do a clone )... (或者可以从一个新文件夹开始并进行克隆)...
This answer doesn't address the question posed, specifically, Keif has answered this , 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 the answer from Keif!我在 Keif 的回答中注意到评论:“您总是希望确保在推动之前先拉动”!
I have also used Git GUI tool in addition to the 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 the command line to do this). (我不确定如何在 Git GUI 中执行相当于命令行命令“git pull origin master”的操作,所以我回到命令行来执行此操作)。
A diagram that shows the various Git commands for various actions that you might want to undertake is this one:下图显示了您可能想要执行的各种操作的各种 Git 命令:
Our workflow is such.我们的工作流程就是这样。 We have a branch called
dev
which I can reach at origin/dev
.我们有一个名为
dev
的分支,我可以通过origin/dev
。 When we do changes, we create a branch off dev:当我们进行更改时,我们会在 dev 之外创建一个分支:
git checkout -b FixForBug origin/dev
Now I have a branch called FixForBug
which is tracking (I think that's the right word) origin/dev
.现在我有一个名为
FixForBug
的分支,它正在跟踪(我认为这是正确的词) origin/dev
。 Thus, if I do a git pull
it'll bring in new changes from origin/dev
which is great.因此,如果我执行
git pull
它会从origin/dev
带来新的变化,这很棒。 Now, when I'm finished with my fix, I push to a remote branch called the same thing.现在,当我完成修复后,我推送到一个名为相同事物的远程分支。
First I pull down any changes from origin/dev
and do a rebase:首先,我从
origin/dev
提取任何更改并进行 rebase:
git pull --rebase
Then I push the changes to a remote branch of the same name:然后我将更改推送到同名的远程分支:
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 ever push anything to
origin/dev
myself.我自己从不向
origin/dev
推送任何东西。 I'm guessing this is as pretty common workflow.我猜这是非常常见的工作流程。
The first time I do a git push
, it works fine and creates the remote branch.我第一次执行
git push
,它工作正常并创建了远程分支。 However, if I push a second time (let's say during code-review, someone points out a problem), I get the following error:但是,如果我第二次推送(假设在代码审查期间有人指出了问题),我会收到以下错误:
error: failed to push some refs to 'https://github.mydomain.info/Product/product.git'
错误:无法将一些引用推送到“https://github.mydomain.info/Product/product.git”
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart.提示:更新被拒绝,因为您当前分支的尖端落后于其远程对应分支。 Integrate the remote changes (eg hint: 'git pull ...') before pushing again.
在再次推送之前集成远程更改(例如提示:'git pull ...')。
See the 'Note about fast-forwards' in 'git push --help' for details.有关详细信息,请参阅“git push --help”中的“关于快进的注意事项”。
However, if I do a git status
it says I'm ahead of origin/dev
by 1 commit (which makes sense) and if I follow the hint and run git pull
, it says everything is up to date.但是,如果我执行
git status
它说我领先于origin/dev
1 次提交(这是有道理的),如果我按照提示运行git pull
,它说一切都是最新的。 I think this is because I'm pushing to a different branch than my upstream branch.我认为这是因为我要推送到与上游分支不同的分支。 I can fix this issue by running:
我可以通过运行来解决这个问题:
git push -f origin FixForBug
In that case, it'll push the changes to the remote branch, saying (forced update) and everything appears to be good on the remote branch.在这种情况下,它会将更改推送到远程分支,说(强制更新)并且远程分支上的一切似乎都很好。
My Questions:我的问题:
Why is -f
required in this scenario?为什么在这种情况下需要
-f
? Usually when you're forcing something, 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?
我这样做可以吗,还是会弄乱远程分支中的某些东西,或者给最终必须将我的东西合并到开发中的人带来麻烦?
It must be because of the commit is ahead of your current push.这一定是因为提交在您当前的推送之前。
git pull origin "name of branch you want to push"
git rebase
If git rebase
is successful, then good.如果
git rebase
成功,那很好。 Otherwise, you have resolve all merge conflicts locally and keep it continuing until the rebase with remote is successful.否则,您已经在本地解决了所有合并冲突并保持它继续,直到使用远程的 rebase 成功。
git rebase --continue
This is how I solved my problem:这就是我解决问题的方法:
Let's assume the upstream branch is the one that you forked from and origin is your repository and you want to send an MR/PR to the upstream branch.假设上游分支是您创建的分支, origin是您的存储库,并且您希望将 MR/PR 发送到上游分支。
You already have, let's say, about four commits and you are getting Updates were rejected because the tip of your current branch is behind.
比方说,您已经有大约四次提交,并且您正在收到
Updates were rejected because the tip of your current branch is behind.
Here is what I did这是我所做的
First, squash all your four commits:首先,压缩所有四个提交:
git rebase -i HEAD~4
You'll get a list of commits with pick
written on them (opened in an editor).你会得到一个提交列表,上面写着
pick
(在编辑器中打开)。
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之后,您可以保存合并的提交
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
This just happened to me.这只是发生在我身上。
Solution: Pull down my own branch so I get that extra commit.解决方案:拉下我自己的分支,以便我获得额外的提交。 Then push it back to my remote branch.
然后将其推回我的远程分支。
On my branch I literally did:在我的分支上,我确实做到了:
git pull
git push
Me help next:接下来我帮忙:
git stash
git pull origin master
git apply
git commit -m "some comment"
git push
In my case, the remote repository already had a branch with the same name as the dev branch that I was working on.就我而言,远程存储库已经有一个与我正在处理的 dev 分支同名的分支。 I just renamed the branch and pushed the code.
我只是重命名了分支并推送了代码。 It worked for me.
它对我有用。
git checkout -b new-branch-name
git push origin new-branch-name
If you use TortoiseGit push dialogue如果你使用 TortoiseGit 推送对话
Cite source: https://tortoisegit.org/docs/tortoisegit/tgit-dug-push.html#id692368引用来源: https ://tortoisegit.org/docs/tortoisegit/tgit-dug-push.html#id692368
known changes - This allows remote repository to accept a safer non-fast-forward push.
已知更改- 这允许远程存储库接受更安全的非快进推送。 This can cause the remote repository to lose commits;
这可能会导致远程存储库丢失提交; use it with care.
小心使用它。 This can prevent from losing unknown changes from other people on the remote.
这可以防止丢失遥控器上其他人的未知更改。 It checks if the server branch points to the same commit as the remote-tracking branch (known changes).
它检查服务器分支是否指向与远程跟踪分支相同的提交(已知更改)。 If yes, a force push will be performed.
如果是,将执行强制推送。 Otherwise it will be rejected.
否则会被拒绝。 Since git does not have remote-tracking tags, tags cannot be overwritten using this option.
由于 git 没有远程跟踪标签,因此无法使用此选项覆盖标签。 This passes --force-with-lease option of git push command.
这通过了 git push 命令的 --force-with-lease 选项。
unknown changes - This allows remote repository to accept an unsafe non-fast-forward push.
未知更改- 这允许远程存储库接受不安全的非快进推送。 This can cause the remote repository to lose commits;
这可能会导致远程存储库丢失提交; use it with care.
小心使用它。 This does not check any server commits, so it is possible to lose unknown changes on the remote.
这不会检查任何服务器提交,因此可能会丢失远程上的未知更改。 Use this option with Include Tags to overwrite tags.
将此选项与包含标签一起使用以覆盖标签。 This passes the traditional --force option of git push command.
这通过了 git push 命令的传统 --force 选项。
If you are really worried about any other approaches, these Steps can help you without any difficutly如果您真的担心任何其他方法,这些步骤可以帮助您轻松
1: Stash your changes in your Local Branch that you want to Push 1:将更改存储在要推送的本地分支中
2: Rename Your Local Branch as your Backup for future 2:将您的本地分支重命名为您未来的备份
3: Create a Branch of the Same name From your Remote that will have all the changes 3:从您的遥控器创建一个同名的分支,该分支将包含所有更改
4: Check out this new Branch as your New Local Branch 4:签出这个新分支作为您的新本地分支
5: Make and Save the Changes in this Branch 5:在此分支中进行并保存更改
6: Commit and Push 6:提交和推送
I had this issue when trying to push after a rebase through Visual Studio Code.我在尝试通过 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.
我的问题是通过从 Git 输出窗口复制命令并从 Visual Studio Code 的终端窗口执行它来解决的。
In my case the command was something like:在我的情况下,命令类似于:
git push origin NameOfMyBranch:NameOfMyBranch
You must have added new files in your commits which has not been pushed.您必须在尚未推送的提交中添加了新文件。 Check the file, push that file again, and then try pull / push.
检查文件,再次推送该文件,然后尝试拉/推。
It will work.它会起作用的。 This worked for me...
这对我有用...
If you tried all of the previous answers and the problem is still not solved, then make sure that the pushed branch name is unique and does not exist in remotes.如果您尝试了之前的所有答案,但问题仍然没有解决,那么请确保推送的分支名称是唯一的,并且在远程中不存在。
The error message might be misleading.错误消息可能具有误导性。
The push were rejected because the tip of your current branch is behind.推送被拒绝,因为您当前分支的尖端落后。
When I had such a situation, I just ran:当我遇到这种情况时,我只是跑了:
git push -f origin main
And it was done.它已经完成了。
Since the branch I was trying to commit was my sub branch under master, I deleted it first from the repository (due to a back referencing issue).由于我尝试提交的分支是我在 master 下的子分支,因此我首先从存储库中将其删除(由于反向引用问题)。 I then retried with push and it worked again!
然后我用 push 重试,它又工作了!
Note: As part of deleting the initial branch, I had all the previous changes in the push I was about to do so no code were lost.注意:作为删除初始分支的一部分,我在即将执行的推送中进行了所有先前的更改,因此没有丢失任何代码。
It depends on the permissions.这取决于权限。
You may not have permission to push directly to a main branch (master, development).您可能无权直接推送到主分支(主分支、开发分支)。 If you are in an enterprise project, you should push your own topic branch to its remote and submit a merge request (MR).
如果您在企业项目中,您应该将自己的主题分支推送到其远程并提交合并请求(MR)。
First, you have to fetch the changes from the remote, only then you will be able to push the changes, In order to do that you have to write the command首先,您必须从远程获取更改,然后才能推送更改,为此您必须编写命令
git pull
after that, you will find some conflict, resolve your conflict and then write the command之后,你会发现一些冲突,解决你的冲突,然后编写命令
git push
This fix is working for me, give it a try and I think due to the conflict on the remote we are not able to push the code此修复程序对我有用,试一试,我认为由于遥控器上的冲突,我们无法推送代码
Check your remote branch isn't locked.检查您的远程分支是否未锁定。
I have encountered this problem in an automated environment only, namely Gitlab pipelines and Github actions, when during releases branches are merged and pushed automatically.我只在自动化环境中遇到过这个问题,即 Gitlab 管道和 Github 操作,在发布期间合并并自动推送分支。 The only manual solution that has worked for me is creating a new branch with the same changes and running the pipeline on the new branch.
唯一对我有用的手动解决方案是创建一个具有相同更改的新分支并在新分支上运行管道。
I haven't yet figured out how to permenently fix it in Gitlab and Github pipelines.我还没有弄清楚如何在 Gitlab 和 Github 管道中永久修复它。
your local master branch: -> HEAD~N -> updated & new files(can't push to github!)您本地的主分支:-> HEAD~N -> 更新和新文件(无法推送到 github!)
your github master branch: -> HEAD~N -> AheadNewCommit1 -> AheadNewCommit2...你的 github 主分支: -> HEAD~N -> AheadNewCommit1 -> AheadNewCommit2 ...
step1.第1步。 reset to the HEAD~N
重置为HEAD~N
$ git reset --soft HEAD~N
step2: pull AheadNewCommit1 -> AheadNewCommit2... to your local master branch step2: pull AheadNewCommit1 -> AheadNewCommit2... 到你本地的master分支
$ git pull origin master
Over here when you are trying to rebase a dev-feature branch from the master branch (Git version for reference >> git version git version 2.37.1 (Apple Git-137.1) )当你试图从 master 分支 rebase 一个 dev-feature 分支时在这里(参考 Git 版本 >> git 版本 git 版本 2.37.1 (Apple Git-137.1) )
1.git checkout dev-feature 1.git 结帐开发功能
checkout the feature branch检查功能分支
2. git pull 2. git拉
update the head to the latest commit将 head 更新为最新的提交
3. git pull --rebase origin master 3. git pull --rebase origin master
set up the rebase flag to pull from the master repo设置 rebase 标志以从主仓库中提取
4. git config pull.rebase true in case the reconcile method is not set, since you have 2 divergent branches 5. git pull Successfully rebased and updated refs/heads/dev-feature** if you get the above message it means the branch has been rebased 4. git config pull.rebase true如果没有设置调和方法,因为你有 2 个不同的分支5. git pull Successfully rebased and updated refs/heads/dev-feature** 如果你收到上面的消息,这意味着分支已经重新定位
6. git push origin dev-feature Push these changes to remote 6. git push origin dev-feature 将这些修改推送到远程
Quick Solution is快速解决方案是
git push -f origin master
git push -f 源主
I dont know why git dont make this clear on their website我不知道为什么 git 不在他们的网站上说清楚
Start by clearing any git commits without changing your code:首先在不更改代码的情况下清除任何 git 提交:
git reset --soft HEAD~1
git reset --soft HEAD~2
etc
git checkout -b tempbranch
Ensure youre going to push to the right place ...确保你要推到正确的地方......
git remote add origin https://github.com/user/examplerepo.git
git remote set-url origin https://github.com/user/examplerepo.git
git add file.txt
after adding the file is now in stating area.添加文件后,现在在声明区域。 Here you can use git status to see what is ready to be committed.
在这里,您可以使用 git status 查看准备提交的内容。 then you can commit:
那么你可以提交:
git commit -m "Put some print statements" file.txt
Now the file in is local repo and you will not see it using "git status".现在文件是本地仓库,你不会使用“git status”看到它。 but you can see the list of committed files using "git log" and "git ls-files"
但是您可以使用“git log”和“git ls-files”查看已提交文件的列表
git push -u origin tempbranch
Next time you can just work on master branch so u can switch to it下次你可以只在 master 分支上工作,这样你就可以切换到它
git checkout master
Also you can check this link for more infohttps://practicalgit.com/blog/do-you-always-need-u-in-push.html您也可以查看此链接以获取更多信息https://practicalgit.com/blog/do-you-always-need-u-in-push.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.