问题描述
$ git push -u origin master
To https://github.com/XXXXXXXXX.com/FirstRepo.git
! [rejected] master -> master (non-fast-forward)
error: Failed to push some refs to 'https://github.com/XXXXXXXXX.com/FirstRepo.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.
我今天是第一次使用github,我已经检查了其他答案,其中只有一个似乎可行,并且正在使用'git push origin master --force',但是这样做是删除所有其他提交。所以任何人都可以告诉我解决方案。
解决方法
如果您的local
分支位于remote
分支的后面,并且在其上面有一些提交,则可以通过运行以下命令来解决问题。
$ git pull origin master --rebase
$ git push origin master
注意事项:如果仅运行git pull
而没有--rebase
,则它将添加您可能不希望的merge
提交。
只需在终端中运行此代码。
git pull origin master --allow-unrelated-histories
或者如果您使用主分支运行此代码
git pull origin main --allow-unrelated-histories