简体   繁体   English

如何解决 git 错误:“更新被拒绝,因为您当前分支的提示落后了”

[英]How to resolve git error: "Updates were rejected because the tip of your current branch is behind"

A well meaning colleague has pushed changes to the Master instead of making a branch.一位好心的同事将更改推送给 Master 而不是创建分支。 This means that when I try to commit I get the error:这意味着当我尝试提交时出现错误:

Updates were rejected because the tip of your current branch is behind更新被拒绝,因为你当前分支的提示落后了

I know this should be resolved by making a pull request to re-sync things but I don't want to lose the changes I have made locally and I equally don't want to force the commit and wipe out the changes made by someone else.我知道这应该通过提出重新同步的拉取请求来解决,但我不想丢失我在本地所做的更改,我同样不想强制提交并清除其他人所做的更改.

What is the correct approach to allow me to merge the changes without losing either?允许我合并更改而不丢失任何更改的正确方法是什么?

If you have already made some commits, you can do the following如果您已经进行了一些提交,您可以执行以下操作

git pull --rebase

This will place all your local commits on top of newly pulled changes.这会将您所有的本地提交置于新拉取的更改之上。

BE VERY CAREFUL WITH THIS : this will probably overwrite all your present files with the files as they are at the head of the branch in the remote repo!对此要非常小心:这可能会用文件覆盖您当前的所有文件,因为它们位于远程存储库中分支的头部! If this happens and you didn't want it to you can UNDO THIS CHANGE with如果发生这种情况并且您不希望它发生,您可以使用UNDO THIS CHANGE

git rebase --abort 

... naturally you have to do that before doing any new commits! ......自然你必须在做任何新的提交之前这样做!

I would do it this this way:我会这样做:

  1. Stage all unstaged changes.暂存所有未暂存的更改。

     git add .
  2. Stash the changes.隐藏更改。

     git stash save
  3. Sync with remote.与遥控器同步。

     git pull -r
  4. Reapply the local changes.重新应用本地更改。

     git stash pop

    or或者

    git stash apply

I had the exact same issue on my branch(lets call it branch B) and I followed three simple steps to get make it work我在我的分支上遇到了完全相同的问题(我们称之为分支 B),我按照三个简单的步骤让它工作

  1. Switched to the master branch (git checkout master)切换到master分支(git checkout master)
  2. Did a pull on the master (git pull)对主人做了拉动(git pull)
  3. Created new branch (git branch C) - note here that we are now branching from master创建了新分支(git branch C) - 注意我们现在是从 master 分支
  4. Now when you are on branch C, merge with branch B (git merge B)现在,当您在分支 C 上时,与分支 B 合并(git merge B)
  5. Now do a push (git push origin C) - works :)现在做一个推送(git push origin C) - 工作:)

Now you can delete branch B and then rename branch C to branch B.现在您可以删除分支 B,然后将分支 C 重命名为分支 B。

Hope this helps.希望这可以帮助。

This worked for me:这对我有用:

git pull origin $(git branch --show-current)
git push

fyi git branch --show-current returns the name of the current branch.仅供参考git branch --show-current返回当前分支的名称。

I had the same problem.我有同样的问题。 Unfortunately I was in wrong catalog level.不幸的是,我的目录级别错误。

I tried to: git push -u origin master -> there was a error我试图: git push -u origin master -> 出现错误

Then I tried: git pull --rebase -> there still was a problem然后我试了: git pull --rebase -> 还是有问题
Finally i change directory cd your_directory最后我改变目录cd your_directory

Then I tried again ( git push ) and it works!然后我再次尝试( git push )并且它有效!

This issue occurs when someone has commited the code to develop/master and latest code has not been rebased from develop/master and you're trying to overwrite new changes to develop/master branch当有人将代码提交到开发/主控并且最新代码尚未从开发/主控重新定位并且您试图覆盖对开发/主控分支的新更改时,会出现此问题

Solution:解决方案:

  • Take a backup if you're working on feature branch and switch to master/develop branch by doing git checkout develop/master如果您正在处理功能分支并通过执行 git checkout develop/master 切换到 master/develop 分支,请进行备份
  • Do git pull做 git pull
  • You will get changes and merge conflicts occur when you have made changes in the same file which has not been rebased from develop/master当您在同一个文件中进行更改时,您将获得更改并发生合并冲突,而该文件尚未从开发/主文件重新定位
  • Resolve the conflicts if it occurs and do git push,this should work如果发生冲突,请解决冲突并执行 git push,这应该可以

I was able to overcome this issue with the following Visual Studio 2017 change:通过以下 Visual Studio 2017 更改,我能够克服此问题:

  1. In Team Explorer, go to Settings .在团队资源管理器中,转到Settings Go to Global Settings to configure this option at the global level;转到全局设置以在全局级别配置此选项; go to Repository Settings to configure this option at the repo level.转到存储库设置以在存储库级别配置此选项。
  2. Set Rebase local branch when pulling to the desired setting (for me it was True ), and select Update to save.拉到所需设置时设置 Rebase 本地分支(对我来说它是True ),然后选择更新保存。

See: https://docs.microsoft.com/en-us/vsts/git/concepts/git-config?view=vsts&tabs=visual-studio#rebase-local-branch-when-pulling请参阅: https ://docs.microsoft.com/en-us/vsts/git/concepts/git-config?view=vsts&tabs=visual-studio#rebase-local-branch-when-pulling

I have used我用过

git push origin master

After update refusing, I looked up at a history:更新拒绝后,我查看了一个历史记录:

git log --oneline --all

My HEAD -> master was above origin/master.我的 HEAD -> master 高于 origin/master。

But I used forcing, and it was sufficient:但是我使用了强制,这就足够了:

git push --force-with-lease origin master

And the heads are together again...头颅又聚在一起了……

You are not currently on a branch.您当前不在分支上。 To push the history leading to the current (detached HEAD) state now, use要将历史推送到当前(分离的 HEAD)状态,请使用

git push origin HEAD:<name-of-remote-branch>

I had the same issue.我遇到过同样的问题。

Fix: git pull origin {branch-name} sorted all.修复: git pull origin {branch-name}全部排序。

Ref: There is no tracking information for the current branch参考: 当前分支没有跟踪信息

This worked for me and i will recommend it to you.这对我有用,我会推荐给你。 if you are on the local branch that you have commited, try renaming the branch with this git command如果您在已提交的本地分支上,请尝试使用此 git 命令重命名分支

git branch -m <new_name>

then push again with然后再次推动

git push --set-upstream origin <new_name>

I had this issue and I realized that .gitignore file is changed.我遇到了这个问题,我意识到 .gitignore 文件已更改。 So, I changed .gitignore and I could pull the changes.所以,我改变了 .gitignore 并且我可以拉动这些改变。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决git错误:重命名远程分支时“更新被拒绝,因为当前分支的提示落后” - How to resolve git error: “Updates were rejected because the tip of your current branch is behind” when renaming a remote branch Git 错误更新被拒绝,因为您当前分支的提示落后 - Git error Updates were rejected because the tip of your current branch is behind git:“更新被拒绝,因为您当前分支的尖端落后..”但是如何查看差异? - git: "Updates were rejected because the tip of your current branch is behind.." but how to see differences? 更新被拒绝,因为您当前分支的提示落后 - 但为什么? - Updates were rejected because the tip of your current branch is behind - but why? 更新被拒绝,因为您当前分支的尖端在后面...不正确 - Updates were rejected, because the tip of your current branch is behind… Not true Git 更新被拒绝,因为您当前分支(主)的尖端在后面但是分支是最新的? - Git Updates were rejected because the tip of your current branch (main) is behind BUT branch is up to date? Github 错误“更新被拒绝,因为您当前分支的提示落后” - Github error "Updates were rejected because the tip of your current branch is behind" 收到错误“更新被拒绝,因为您当前分支的提示落后了” - Getting error “Updates were rejected because the tip of your current branch is behind” 如何解决“由于当前分支的尖端落后而导致更新被拒绝”? - How can I solve “Updates were rejected because the tip of your current branch is behind”? 在&#39;git filter-branch`之后无法推送到Heroku:更新被拒绝,因为当前分支的提示落后了 - Can't push to Heroku after a `git filter-branch`: Updates were rejected because the tip of your current branch is behind
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM