我昨天还能推,现在却推不动了。

当我使用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.

我的工作目录和远程存储库看起来像什么:


当前回答

我在GitHub中创建了一个空的存储库,并在本地有我的代码。我现在面临着同样的问题,我按照下面的顺序,

git init
git commit -m 'Initial Commit'
git remote add origin https://github.com/kavinraju/Repo-Name.git
git add .
git push -u origin master

问题是:我试图在暂存我拥有的文件之前提交。

所以我们需要暂存文件,然后提交。

这是正确的顺序。

git init
git add .
git commit -m 'Initial Commit'
git remote add origin https://github.com/kavinraju/Repo-Name.git
git push -u origin master

因为我首先执行了错误的序列,所以我只执行了下面的命令:

git add .
git commit -m 'Initial Commit'
git push -u origin master

其他回答

记得在推送到GitHub存储库之前提交您的更改。这可能会解决你的问题。

我在GitHub帮助(处理非快进错误)中找到了这个问题的解决方案:

你可以通过获取并合并在远程分支上所做的更改和你在本地所做的更改来修复这个问题: $ git获取原点 获取在线存储库的更新 $ git合并起源分支 #将在线更新与本地工作合并 或者,你可以简单地使用git pull一次执行这两个命令: $ git拉起源分支 #获取在线更新,并将其与您的本地工作合并

重命名分支,然后推送,例如:

git branch -m new-name
git push -u new-name

这对我很管用。

这些步骤对我很有效:

切换到当前分支和拉最新的代码 重命名本地分支 Git分支-m [new-name] 将本地分支推到服务器 Git push origin [new-name] 从服务器中删除分支 Git push origin——delete [old-name]

事实上,GitHub将master改为main让我遇到了这个问题。所以从现在开始,推到原点的解决方案是:

git push -u origin main