无法将本地更改推送到现有的远程分支

7

有一个名为“my-remote”的远程分支,我之前可以轻松地推送到该分支。但今天,我无法进行推送,并遇到了不同的错误。

首先我遇到的错误是:

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.

我做了一些研究并运行了这个,希望它能解决问题:

git config push.default tracking

运行完后,再次执行push命令:

git push https://github.com/someurl/mybranch.git

我收到了以下错误信息:

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.

我尝试运行了这个代码:
git push master:my-remote https://github.com/someurl/mybranch.git

但它告诉我:
fatal: remote part of refspec is not a valid name in https://github.com/someurl/mybranch.git
2个回答

8
如果你想将你的主分支推送到my-remote远程分支,正确的语法应该是:
 git push https://github.com/someurl/mybranch.git master:my-remote 

首先:远程仓库引用和refspec是指从git push man page中得到的。

关于你收到的第一个错误信息,如果它没有告诉你需要合并代码,那么可能需要执行git pull --rebase命令。
或者至少执行以下命令:

 git config --global push.default current

(如"配置Git只推送当前分支"所述)。

如果您在本地分支'my-remote'中(根据命令),您可以通过执行以下操作确保上游分支已设置:

git push -u https://github.com/someurl/mybranch.git my-remote:my-remote

谢谢,我运行了这个命令,但是还是出现了错误:error: failed to push some refs to 'https://github.com/newcolabs/relay_contractors.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. 我很确定自从上次提交以来没有人编辑过这个分支。在github上我也看不到任何新的提交。我该如何合并? - user1404536
@user1404536,我对那个答案进行了另外两次编辑,包括一个git pull --rebase选项,它应该将您的本地分支放在远程分支之上。 - VonC
当我运行 git pull --rebase 命令时,它告诉我分支已经是最新的。 - user1404536
@user1404536,那么git push -u https://github.com/someurl/mybranch.git my-remote:my-remote是什么意思呢?(为了明确设置上游分支,就像https://dev59.com/pW025IYBdhLWcg3w9qyQ中所述) - VonC
这很尴尬,但在某个地方,似乎我的更改在出现错误时被提交了(我刚在GitHub上看到)。你要求我运行的最后一个命令是:“一切都是最新的”。 - user1404536
显示剩余3条评论

0

将代码推送到现有的git仓库需要9个步骤。

我尝试了"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://User@bitbucket.org':
To https://User@bitbucket.org/User/app.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://User@bitbucket.org/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.

然后我尝试了 "git pull" 并获取了最新的更改:

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git pull
Password for 'https://User@bitbucket.org':
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>

如果您希望为此分支设置跟踪信息,可以使用以下方法:

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

我执行了 "git push" 命令,但更改失败了:

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://User@bitbucket.org':
To https://User@bitbucket.org/User/app.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://User@bitbucket.org/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"和"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

在主分支上,没有需要提交的内容,工作目录干净。

执行 "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.

然而,“git push origin master”没有起作用,因为当前分支的尖端落后于它的远程对应分支。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://User@bitbucket.org':
To https://User@bitbucket.org/User/app.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://User@bitbucket.org/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 pull" 命令来合并代码库。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git pull
Password for 'https://User@bitbucket.org':
Merge made by the 'recursive' strategy.

执行了 "git pull" 后,在 Visual Studio git 插件中,"git push origin master" 正是需要的来使同步命令工作的。
C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>git push origin master
Password for 'https://User@bitbucket.org':
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://User@bitbucket.org/User/app.git
   40d72a2..9748b8b  master -> master

C:\Users\User\Documents\Visual Studio 2013\Projects\app\User-app-40d72a288916>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接