git 无法将本地更改推送到现有远程分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12341508/
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
Can't push local changes to an existing remote branch
提问by user1404536
There is a remote branch called "my-remote" that I had previously pushed to with no problem. As of today, I can't push and I get different errors.
有一个名为“my-remote”的远程分支,我之前已经毫无问题地推送到了它。截至今天,我无法推送,并且出现不同的错误。
First error I got was:
我得到的第一个错误是:
hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. If you did not intend to push that branch, you may want to hint: specify branches to push or set the 'push.default' configuration hint: variable to 'current' or 'upstream' to push only the current branch.
hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. If you did not intend to push that branch, you may want to hint: specify branches to push or set the 'push.default' configuration hint: variable to 'current' or 'upstream' to push only the current branch.
I did some research and ran this hoping it would solve the problem:
我做了一些研究并运行它,希望它能解决问题:
git config push.default tracking
after running that I ran the push again:
运行后,我再次运行推送:
git push https://github.com/someurl/mybranch.git
I got the following error:
我收到以下错误:
pushing to remote 'https://github.com/someurl/mybranch.git', which is not the upstream of
your current branch 'my-remote', without telling me what to push
to update which remote branch.
I have tried running this:
我试过运行这个:
git push master:my-remote https://github.com/someurl/mybranch.git
but it tells me that:
但它告诉我:
fatal: remote part of refspec is not a valid name in https://github.com/someurl/mybranch.git
回答by VonC
If you want to push your master branch on the my-remote remote branch, the correct syntax would be:
如果你想在 my-remote 远程分支上推送你的主分支,正确的语法是:
git push https://github.com/someurl/mybranch.git master:my-remote
(first: remote repo reference, the refspec, from git push
man page)
(第一:远程仓库参考,refspec,来自git push
手册页)
Regarding your first error message, if it really didn't tell you to merge, then a git pull --rebase
might have been needed.
Or at least a:
关于你的第一条错误消息,如果它真的没有告诉你合并,那么git pull --rebase
可能需要一个。
或者至少一个:
git config --global push.default current
(As mentioned in "Configure Git to Only Push Current Branch").
(如“配置 Git 以仅推送当前分支”中所述)。
If you are in the local branch 'my-remote
' (which is the case, according to the command), you can make sure the upstream branch is set by making a:
如果您在本地分支 ' my-remote
'(就是这种情况,根据命令),您可以通过执行以下操作来确保上游分支已设置:
git push -u https://github.com/someurl/mybranch.git my-remote:my-remote
回答by Tyrone Moodley
There are 9 steps to pushing to an existing git repo.
推送到现有 git 存储库有 9 个步骤。
I tried "git push --set-upstream origin master" and got the following error:
我尝试了“git push --set-upstream origin master”并得到以下错误:
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push --set-upstream origin master
Password for 'https://[email protected]':
To https://[email protected]/User/app.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://[email protected]/User/
app.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I then tried "git pull" and got the lastest changes:
然后我尝试了“git pull”并获得了最新的更改:
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git pull
Password for 'https://[email protected]':
warning: no common commits
remote: Counting objects: 344, done.
remote: Compressing objects: 100% (275/275), done.
remote: Total 344 (delta 45), reused 336 (delta 41)
Receiving objects: 100% (344/344), 15.91 MiB | 43.00 KiB/s, done.
Resolving deltas: 100% (45/45), done.
From https://bitbucket.org/User/app
* [new branch] master -> origin/master
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
如果您希望为此分支设置跟踪信息,您可以这样做:
git branch --set-upstream-to=origin/<branch> master
I did a "git push" and the changes failed:
我做了一个“git push”,但更改失败:
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://[email protected]':
To https://[email protected]/User/app.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/User/
app.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.
"git add -A" and "git commit" did not work because there was nothing to commit.
“git add -A”和“git commit”不起作用,因为没有什么可提交的。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git add -A
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git commit
On branch master nothing to commit, working directory clean
在分支主不需要提交,工作目录干净
"git branch --set-upstream-to=origin/master master" seemed to do the trick.
“git branch --set-upstream-to=origin/master master”似乎可以解决问题。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git branch --set-upstream-to=origin/master master
Branch master set up to track remote branch master from origin.
However, a "git push origin master" didn't work because the tip of the current branch is behind it remote counterpart.
但是,“git push origin master”不起作用,因为当前分支的尖端在其远程对应分支的后面。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://[email protected]':
To https://[email protected]/User/app.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/User/
app.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.
A "git pull" was needed to merge the repo's.
需要一个“git pull”来合并回购。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git pull
Password for 'https://[email protected]':
Merge made by the 'recursive' strategy.
After executing the "git pull", "git push origin master" was exactly what was needed to get the sync commanded working in Visual Studio git plugin.
执行“git pull”后,“git push origin master”正是让同步命令在 Visual Studio git 插件中工作所需要的。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://[email protected]':
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 539 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To https://[email protected]/User/app.git
40d72a2..9748b8b master -> master
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>