简体   繁体   English

git:“更新被拒绝,因为您当前分支的尖端落后..”但是如何查看差异?

[英]git: "Updates were rejected because the tip of your current branch is behind.." but how to see differences?

I just finished working on a piece of code.我刚刚完成了一段代码。 Wanted to push and got the already famous:想推并得到已经有名的:

hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart.提示:更新被拒绝,因为您当前分支的提示落后于提示:它的远程对应部分。 Integrate the remote changes (eg hint: 'git pull ...') before pushing again.在再次推送之前集成远程更改(例如提示:'git pull ...')。

Now I've seen this question posted several times here, eg现在我已经在这里多次看到这个问题,例如

Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. 更新被拒绝,因为您当前分支的尖端在提示后面:它的远程对应部分。 Integrate the remote changes (eg 集成远程更改(例如

Updates were rejected because the tip of your current branch is behind 更新被拒绝,因为您当前分支的尖端落后

According to the specific case, the solution is either to根据具体情况,解决方案是

  • git pull , so the remote changes are merged on to my local work, OR git pull ,因此远程更改将合并到我的本地工作中,或者
  • git push -f , a force push to update the remote (origin) branch. git push -f ,强制推送更新远程(源)分支。

Now, it has been a while I haven't worked on this branch.现在,我已经有一段时间没有在这个分支上工作了。 I don't necessarily want to merge the remote changes onto my current work!我不一定要将远程更改合并到我当前的工作中! Nor do I know if I can safely force the update on the origin branch...我也不知道我是否可以安全地强制源分支上的更新......

How can I just see the differences and decide which is best for my case?我如何才能看到差异并决定哪个最适合我的情况?

in order to see the differences, first you need to fetch the commits from the origin repository:为了查看差异,首先您需要从原始存储库中获取提交:

git fetch origin

Now you can see the diffs (Assuming you are on the master branch) git diff HEAD..origin/master现在您可以看到差异(假设您在 master 分支上) git diff HEAD..origin/master

Now you are armed with the knowledge you seek to decide to merge or rebase before push ing your changes.现在,您已经掌握了在push更改之前决定mergerebase的知识。

I had this happen to me recently when I created a new branch with git checkout -b feature/abc , committed some changes, and then tried git push --set-upstream origin feature/abc it to create a pull request for review.最近,当我使用git checkout -b feature/abc创建一个新分支,提交了一些更改,然后尝试git push --set-upstream origin feature/abc它创建一个拉取请求以供审查时,我遇到了这种情况。 The error occurred because the remote branch already existed when I thought I was defining the branch locally.发生错误是因为当我认为我在本地定义分支时远程分支已经存在。 Deleting the remote branch resolved the issue and my push succeeded.删除远程分支解决了这个问题,我的推送成功了。

If you mean to discard your local changes you should run git reset --hard @{u} .如果您打算放弃本地更改,您应该运行git reset --hard @{u} Note again this is irreversible action wrt some data, so be sure before running it.再次注意,这是对某些数据的不可逆操作,因此在运行之前请确保。 Here's how:就是这样:

To review which local commits you currently have you can use git log HEAD --not --remotes , to compare to any remote branch or git log @{u}..HEAD to see differences specific to the tracked branch.要查看您当前拥有的本地提交,您可以使用git log HEAD --not --remotes与任何远程分支进行比较或git log @{u}..HEAD以查看特定于跟踪分支的差异。

To see the actual diff which you have committed locally, run git diff @{u}... .要查看您在本地提交的实际差异,请运行git diff @{u}... This will ignore remote progress, and show only your changes.这将忽略远程进度,并仅显示您的更改。

To see uncommitted changes, run git diff HEAD .要查看未提交的更改,请运行git diff HEAD

PS: You should run first git fetch origin or git remote update to update the tracking references. PS:您应该首先运行git fetch origingit remote update来更新跟踪参考。

I wanted to add a scenario that happened to me recently where this error shows up seemingly out of nowhere, even when you do everything right.我想添加一个最近发生在我身上的场景,即使你做对了所有事情,这个错误似乎也突然出现。 The cause wasn't obvious but finally seemed to be the change in default push behavior since git version 2.0.原因并不明显,但最终似乎是自 git 2.0 版以来默认推送行为的变化。 I was collaborating with someone using git version 1.8 and I had version 2.x.我正在与使用 git 1.8 版的人合作,我有 2.x 版。 So although we were both doing git push, different types of push were happening.所以虽然我们都在做 git push,但不同类型的推送正在发生。 For us, the problem resolved once we changed the default push behavior by simply running this command on git version 1.8 computer-对我们来说,一旦我们通过在 git 版本 1.8 计算机上运行此命令来更改默认推送行为,问题就解决了——

git config --global push.default simple

This changes the push.default of git version >=1.7 to same as for version 2.0 onwards.这会将 git 版本 >=1.7 的 push.default 更改为与 2.0 以后的版本相同。 For lower versions of git, you might need another default behavior.对于较低版本的 git,您可能需要另一个默认行为。 We both are new to git so correct me if I am wrong but this did made our "Updates were rejected..." error go away.我们都是 git 新手,所以如果我错了请纠正我,但这确实使我们的“更新被拒绝......”错误消失了。

Also, if there is a better venue for this answer, let me know and I'll be happy to move it to somewhere else.另外,如果这个答案有更好的场所,请告诉我,我很乐意将其移至其他地方。

暂无
暂无

声明:本站的技术帖子网页,遵循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" 更新被拒绝,因为您当前分支的提示落后 - 但为什么? - 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? 如何解决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 如何解决“由于当前分支的尖端落后而导致更新被拒绝”? - How can I solve “Updates were rejected because the tip of your current branch is behind”? 在'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 源树推送:提示:更新被拒绝,因为当前分支的尖端落后 - Source Tree Push : hint: Updates were rejected because the tip of your current branch is behind Github 错误“更新被拒绝,因为您当前分支的提示落后” - Github error "Updates were rejected because the tip of your current branch is behind"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM