当我试图将我的代码推送到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.

我还没有在存储库中推入任何东西,那么为什么我需要拉出一些东西呢?


当前回答

使用push命令的问题是本地和远程存储库不匹配。如果你在从git hub创建新的存储库时默认初始化了自述库,那么主分支会自动创建。然而,当你试图推动,没有任何分支。你不能推…因此,最好的做法是创建没有默认自述me初始化的repo。

其他回答

有些人可能会遇到这个错误,因为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

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

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

您将无法将更改推送到远程分支,除非您取消阶段性文件,然后保存本地更改,并应用来自远程的pull,然后您可以将更改推送到远程。

步骤如下—> .单击“确定”

git重置-soft HEAD~1(获取阶段性文件)

Git状态(检查暂存的文件)

Git恢复—分段<文件..>(恢复分级)

Git存储(保存当前更改)

Git pull(从远程获取更改)

Git stash apply(应用本地更改,以便添加和提交)

Git add <files…>(添加本地文件提交)

Git commit -m commit msg

git push

这可能导致远程存储库丢失提交;小心使用。

如果你不希望将远程分支合并到本地分支(参见git diff的区别),并且想要强制推送,请使用push命令和-f

git push -f origin <branch>

原点是远程回购的名称。

通常,该命令拒绝更新不是用于覆盖它的本地引用的祖先的远程引用。这个标志禁用检查。这可能导致远程存储库丢失提交;小心使用。

当我将最新的更改推到我用于gitweb的裸Git存储库时,我得到了类似的错误。在我的例子中,我没有在裸库中做任何更改,所以我只是删除了我的裸库并再次克隆:

git clone --bare <source repo path> <target bare repo path>