(Note: 2020 年 10 月开始 https://github.blog/changelog/2020-08-26-set-the-default-branch-for-newly-created-repositories/,任何新存储库都是使用默认分支创建的main
, not master
。你可以将现有存储库默认分支重命名为master to main https://github.com/github/renaming.
2014 年答案的其余部分已更新为使用“main
")
(以下假设github.com
本身就是not向下,如eri0o https://stackoverflow.com/users/965638/eri0o指出在评论 https://stackoverflow.com/questions/24114676/git-error-failed-to-push-some-refs-to-remote/24114760#comment121465079_24114676: see www.githubstatus.com https://www.githubstatus.com/为了确定)
如果 GitHub 存储库已看到新的提交,而您在本地工作时,我建议使用:
git pull --rebase
git push
完整的语法是:
git pull --rebase origin main
git push origin main
使用 Git 2.6+ https://stackoverflow.com/a/30209750/6309(2015年9月),完成(一次)后
git config --global pull.rebase true
git config --global rebase.autoStash true
一个简单的git pull
就足够了。
(注:与Git 2.27 2020 年第二季度 https://stackoverflow.com/a/61562652/6309, a merge.autostash
也可用于您的常规拉取,无需重新设置)
这样,您就可以重播(--rebase
部分)您在新更新的本地提交origin/main
(or origin/yourBranch
: git pull origin yourBranch
).
请参阅更完整的示例第 6 章 使用 rebase 拉取 http://chimera.labs.oreilly.com/books/1230000000561/ch06.html#pull-rebase of the Git 袖珍书 http://chimera.labs.oreilly.com/books/1230000000561.
我会推荐一个:
# add and commit first
#
git push -u origin main
# Or git 2.37 Q2 2022+
git config --global push.autoSetupRemote true
git push
这将在您的本地主分支与其上游分支之间建立跟踪关系。
之后,将来对该分支的任何推送都可以通过简单的操作完成:
git push
再次,与Git 2.37+ 及其全局选项push.autoSetupRemote https://stackoverflow.com/a/72401899/6309, 一个简单的git push
即使对于第一个也会做同样的事情(即:在本地之间建立跟踪关系main
分支及其上游分支origin/main
).
See "为什么我需要显式推送一个新分支? https://stackoverflow.com/a/17096880/6309".
既然OP已经重置并重做其提交 https://stackoverflow.com/a/18589043/6309在之上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
.
Note: git reset --mixed origin/main
也可以写成git reset origin/main
,自从--mixed
使用时选项为默认选项git reset http://git-scm.com/docs/git-reset.