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

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

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

gitbitbucketgit-merge

提问by Finglish

I recently started using Git (previously I used subversion but I am now doing some collaborative work on a project that uses bitbucket and git).

我最近开始使用 Git(以前我使用 subversion,但我现在正在对一个使用 bitbucket 和 git 的项目进行一些协作工作)。

All has been going well up until today when I find a well meaning colleague has pushed changes to the Master instead of making a branch. This means that when I try to commit I get the error:

一切都很顺利,直到今天我发现一位善意的同事将更改推送到 Master 而不是创建分支。这意味着当我尝试提交时出现错误:

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?

允许我合并更改而不丢失任何更改的正确方法是什么?

回答by Akash Agrawal

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 CHANGEwith

非常小心:这可能会用文件覆盖您当前的所有文件,因为它们位于远程存储库中分支的头部!如果发生这种情况,你不希望它可以撤消此更改

git rebase --abort 

... naturally you have to do that before doing any new commits!

...自然地,您必须在进行任何新提交之前这样做!

回答by Rebecca Abriam

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
    
  1. 暂存所有未暂存的更改。

    git add .
    
  2. 隐藏更改。

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

    git pull -r
    
  4. 重新应用本地更改。

    git stash pop
    

    或者

    git stash apply
    

回答by Chetan Chandrashekar

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

Now you can delete branch B and then rename branch C to branch B.

现在您可以删除分支 B,然后将分支 C 重命名为分支 B。

Hope this helps.

希望这可以帮助。

回答by Pawe? R

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
Finally i change directory cd your_directory

然后我尝试了:git pull --rebase-> 还是有问题
最后我更改了目录cd your_directory

Then I tried again ( git push) and it works!

然后我又试了一次 ( git push) 并且成功了!

回答by Taersious

I was able to overcome this issue with the following Visual Studio 2017 change:

我能够通过以下 Visual Studio 2017 更改克服此问题:

  1. In Team Explorer, go to Settings. Go to Global Settingsto configure this option at the global level; go to Repository Settingsto configure this option at the repo level.
  2. Set Rebase local branch when pullingto the desired setting (for me it was True), and select Updateto save.
  1. 在团队资源管理器中,转到设置。转到全局设置以在全局级别配置此选项;转到存储库设置以在存储库级别配置此选项。
  2. 在拉到所需设置设置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