尝试推送到远程分支时出现 git 错误

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

git error when trying to push to remote branch

gitpush

提问by user429400

I've cloned repository A's master branch from git and created my own branch called Li. I've made some changes a while ago and pushed the contents of local Li to remote Li.

我已经从 git 克隆了存储库 A 的 master 分支,并创建了我自己的名为 Li 的分支。前段时间做了一些改动,把本地Li的内容推送到远程Li。

Now I've pulled some updates from remote master to my local master branch and from the local master branch to the local Li, and I'm trying to push the updates from local Li to remote Li. However, when I try to run:

现在我已经将一些更新从远程 master 拉到了本地 master 分支,从本地 master 分支拉到了本地 Li,我正在尝试将更新从本地 Li 推送到远程 Li。但是,当我尝试运行时:

git checkout Li
git push origin Li

I get the following error:

我收到以下错误:

error: failed to push some refs to '[email protected]:anodejs/system.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Note that my local master branch is updated (I invoked git pull origin master) and merged into the local Li branch. I did, however, add local Li a new file, so local Li is not identical to local master (but this shouldn't matter, right?)

请注意,我的本地 master 分支已更新(我调用了 git pull origin master)并合并到本地 Li 分支中。但是,我确实为本地 Li 添加了一个新文件,因此本地 Li 与本地 master 不同(但这应该无关紧要,对吧?)

Thanks, Li

谢谢,李

采纳答案by Christopher

Find the diff with git fetch && git log Li..origin/Li. I would guess you've rebased or otherwise recut Lisince last time you pushed, but that command should tell you exactly what's in the remote that isn't in the local. You can find what's in either (but not both) with the triple-dot syntax: git log Li...origin/Li.

用 找出差异git fetch && git log Li..origin/Li。我猜你Li自上次推送以来已经重新定位或以其他方式重新切割,但该命令应该告诉你远程中没有本地的内容。您可以使用三点语法找到其中一个(但不是两个)中的内容:git log Li...origin/Li.

If the diff is expected, then just merge with git merge origin/Liand then git push. If the change is unwanted, overwrite the remote with git push -f origin Li. Only do this if you want to abandon the remote's changes.

如果差异是预期的,那么只需与git merge origin/Li然后合并git push。如果不需要更改,请使用 覆盖遥控器git push -f origin Li。仅当您想放弃遥控器的更改时才这样做。

回答by Siva Kandaraj

Updates were rejected because a pushed branch tip is behind its remote.

更新被拒绝,因为推送的分支提示在其远程后面。

git config --global push.default current

Try the command above to set the current branch as default and

尝试上面的命令将当前分支设置为默认值并

git push

回答by FunkyKat

You can force push changes to remote address, but it can produce new remote branch

您可以强制将更改推送到远程地址,但它可以生成新的远程分支

git push subtree_remote_address.git `git subtree split --prefix=subtree_folder`:refs/heads/branch --force

回答by Hossain Mahmood Tuhin

git branch --set-upstream-to=origin/ master

git branch --set-upstream-to=origin/master

If you are having the error when you are on 'master' branch, try the following command:

如果您在“master”分支上遇到错误,请尝试以下命令:

git branch --set-upstream-to=origin/master

git 分支 --set-upstream-to=origin/master

Or, a shortcut: git push -u origin master

或者,一个快捷方式: git push -u origin master