我现在不能推送,尽管我昨天可以推送。
当我使用 git push origin master
时,出现错误:
$ git remote -v
origin https://github.com/REDACTED.git (fetch)
origin https://github.com/REDACTED.git (push)
$ git push origin master
Username for 'https://github.com': REDACTED
Password for 'https://REDACTED@github.com':
To https://github.com/REDACTED.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/REDACTED.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
我的工作目录和远程存储库是什么样的:
最佳答案
(注意:starting Oct. 2020,任何新的存储库都是使用默认分支 main
创建的,而不是 master
。您可以 rename existing repository default branch from master
to main
。
2014 年答案的其余部分已更新为使用“main
”)
(以下假设 github.com
本身没有关闭,正如 eri0o 在 the comments 中指出的那样:请参阅 www.githubstatus.com
确定)
如果 GitHub 存储库看到新的提交推送到它,而你在本地工作,我建议使用:
git pull --rebase
git push
完整的语法是:
git pull --rebase origin main
git push origin main
With Git 2.6+ (2015年9月),完成后(一次)
git config --global pull.rebase true
git config --global rebase.autoStash true
一个简单的 git pull
就足够了。
(注意:使用 Git 2.27 Q2 2020 时,merge.autostash
也可用于您的常规 pull ,无需 rebase )
这样,您将在新更新的 origin/main
(或 origin/yourBranch)之上重播(
: --rebase
部分)您的本地提交git pull origin yourBranch
).
请参阅 chapter 6 Pull with rebase 中的更完整示例的 Git Pocket Book .
我会推荐:
# add and commit first
#
git push -u origin main
# Or git 2.37 Q2 2022+
git config --global push.autoSetupRemote true
git push
这将在您的本地主分支与其上游分支之间建立跟踪关系。
之后,该分支的任何 future 推送都可以通过简单的方式完成:
git push
同样,Git 2.37+ and its global option push.autoSetupRemote
,一个简单的 git push
即使是第一个也会做同样的事情(即:在你的本地 main
分支和它的上游分支 origin/main 之间建立一个跟踪关系
)。
参见“Why do I need to explicitly push a new branch?”。
由于 OP 已经 reset and redone its commit在 origin/main
之上:
git reset --mixed origin/main
git add .
git commit -m "This is a new commit for what I originally planned to be amended"
git push origin main
不需要pull --rebase
。
注意:git reset --mixed origin/main
也可以写成git reset origin/main
,因为--mixed
选项是使用 git reset
时的默认值.
关于git 错误 : failed to push some refs to remote,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114676/