当我试图将我的代码推送到GitHub时,我遇到了一些问题。

Pushing to git@github.com:519ebayproject/519ebayproject.git
To git@github.com:519ebayproject/519ebayproject.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:519ebayproject/519ebayproject.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提交并尝试在git拉到其他人已经做了更改的分支x之前进行git推送更改时。

正常的流程如下:

步骤1:git将本地未提交的更改保存在该分支上。

步骤2:git拉origin branch_name -v来拉并合并到本地提交的分支上的更改(给这个合并一些消息,如果有的话修复冲突)。

步骤3:git隐藏弹出隐藏的更改(然后你可以在弹出的文件上提交,如果你想要,或者先推送已经提交的更改(STEP4),然后再将新的提交到文件中。)

步骤4:git推送origin branch_name -v合并后的更改。

将branch_name替换为master(用于主分支)。

其他回答

我在我的教程《如何使用GitHub:初学者教程》中提到了这一点。

当你在GitHub上创建一个新的存储库时,GitHub可能会要求你创建一个自述文件。如果你直接在GitHub上创建自述文件,那么你需要首先在“推送”请求成功之前发出“拉”请求。 这些命令将“拉”远程存储库,将其与当前文件合并,然后将所有文件“推”回GitHub:

git pull https://github.com/thomas07vt/MyFirstRepo.git master

git push https://github.com/thomas07vt/MyFirstRepo.git master

我遇到了同样的问题,结果是我在一个不同的(本地)分支上,而不是我认为的,正确的本地分支在远程提交时落后了。

我的解决方案:签出正确的分支,从其他本地分支中选择提交,git拉和git推

Git push -f origin branchname

只有当您确定不需要远程分支代码时,才使用上述命令,否则请先合并,然后再推送代码

推送前是否更新了代码?

在你推送任何东西之前使用git pull origin master。

我假设您正在使用origin作为遥控器的名称。

你需要先拉后推,在你推送一些东西之前让你的本地存储库更新(以防其他人已经在github.com上更新了代码)。这有助于在局部解决冲突。

有些人可能会遇到这个错误,因为Git不知道您要推哪个分支。

如果您的错误消息还包括

error: failed to push some refs to 'git@github.com:jkubicek/my_proj.git'
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.

那么你可能想要遵循Jim Kubicek的提示,将Git配置为只推送当前分支,将默认分支设置为当前分支。

git config --global push.default current