【问题标题】:Can't push local changes to an existing remote branch无法将本地更改推送到现有的远程分支
【发布时间】:2012-09-02 17:14:59
【问题描述】:

有一个名为“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

运行后我再次运行推送:

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

【问题讨论】:

    标签: git remote-branch


    【解决方案1】:

    如果你想将你的 master 分支推送到 my-remote 远程分支上,正确的语法应该是:

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

    (first: remote repo reference, refspec, from git push man page)

    关于您的第一条错误消息,如果它确实没有告诉您合并,那么可能需要 git pull --rebase
    或者至少是:

     git config --global push.default current
    

    (如“Configure Git to Only Push Current Branch”中所述)。

    如果您在本地分支'my-remote'(根据命令是这种情况),您可以通过以下方式确保设置上游分支:

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

    【讨论】:

    • 谢谢,我运行了这个,但我仍然得到这个错误:错误:未能将一些参考推送到 'github.com/newcolabs/relay_contractors.git' 提示:更新被拒绝,因为您当前分支的提示落后于提示:其远程对应。合并远程更改(例如“git pull”)提示:在再次推送之前。我很确定自从我上次提交以来没有人编辑这个分支。我在 github 上看不到任何新的提交。如何合并?
    • @user1404536 我对该答案进行了另外 2 处编辑,包括一个 git pull --rebase 选项,该选项应该将您的本地分支放在远程分支之上。
    • 当我运行 git pull --rebase 它告诉我分支是最新的
    • @user1404536 git branch -a 返回什么(换句话说,您当前的本地和远程分支是什么?)。有点像stackoverflow.com/a/10422740/6309
    • running git branch -a 显示所有远程分支的列表,my-remote 旁边有一个星号。
    【解决方案2】:

    推送到现有的 git repo 需要 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”来合并 repo。

    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”后,“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://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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-09
      • 2022-07-09
      • 2020-06-11
      • 2016-10-25
      • 2021-09-17
      • 2017-02-08
      • 2011-03-13
      相关资源
      最近更新 更多