git - 推送到新的远程分支时更新被拒绝错误

标签 git github

我已经 fork 了一个 github 存储库,现在我的 fork 提前了几个提交。我现在想将这些提交中的一个作为 PR 提供给原始存储库。

正在关注 this question's answer ,我做了:

git remote add official [URL to original repo]
git checkout -b hotfix-for-feature official/master
git cherry-pick [feature-hash]
git push -u origin hotfix-for-feature

我得到:

! [rejected]        hotfix-for-feature -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/myusername/repositoryname'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

注意如何:

  1. git push -u 的分支参数被完全忽略,它想推送到 origin/master
  2. 远程不存在 origin/hotfix-for-featureofficial/hotfix-for-feature
  3. official/master 在任何方面都没有领先 - 我真的只是获取了它

如果我尝试 git branch hotfix-for-feature --set-upstream=origin/hotfix-for-feature 我被告知 git push -u 是正确的方法来做到这一点。如果我尝试 git push -u origin/hotfix-for-feature 我得到:

fatal: You are pushing to remote 'origin/hotfix-for-feature',
which is not the upstream of your current branch 'hotfix-for-feature',
without telling me what to push to update which remote branch.

这 - 除了是如何不写错误消息的一个很好的例子 - 我不明白。我指定要推送的内容(当前分支)以及要更新的远程分支。

我发现了很多关于这个错误的问题,但它总是关于某个远程分支在前面,在这种情况下,没有现有的远程分支。另外,“推送分支”和“远程 [分支]” 之间有什么区别?

最佳答案

您收到以下错误消息:

! [rejected]        hotfix-for-feature -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/myusername/repositoryname'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

最重要的是

hotfix-for-feature -> master

您正在尝试推送本地分支 hotfix-for-feature到远程分支 master .

The branch argument to git push -u is just completely ignored and it wants to push to origin/master

它不会被忽略。它推送本地分支hotfix-for-feature到远程分支 master .

doculentation of git push 意味着使用 refspec :

Specify what destination ref to update with what source object. The format of a parameter is an optional plus +, followed by the source object , followed by a colon :, followed by the destination ref .

...

在第二个参数中,您不仅可以指定要推送的(本地)分支,还可以指定要推送到的(git push <remote> <sourcebranch>:<destinationbranch>)分支。

了解这一点,您可以使用 git push -u origin hotfix-for-feature:hotfix-for-feature用于推送本地分支 hotfix-for-feature到远程分支 hotfix-for-feature .

关于git - 推送到新的远程分支时更新被拒绝错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71209460/

相关文章:

ios - Pod 仓库推送私有(private)仓库遇到错误 : Repository not found

android - 如何在 Gradle 中指定最新的提交版本

github - 当触发器是发布时,GitHub 的上传发布 Assets 操作的 upload_url?

git - 如何将 SSH key 上传到 Visual Studio Team Services(用于 Git 存储库)

git - 撤消 git 中的更改(不重写历史记录)

svn - 如何从存储库中获取中继代码? (红矿,svn)

GIT 新手 : how to check what has been changed in my case?

git - Jenkins 和 Stash 设置 ssh key

git - 使用 git rebase 编辑 merge 提交

javascript - Gulp.js GUI 与 Electron