git 错误 : failed to push some refs to remote

标签 git github

我现在不能推送,尽管我昨天可以推送。

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

我的工作目录和远程存储库是什么样的:

Screenshot of Windows file folder with these directories: .git, css, js. And these files: index.php, readme, setsu.php. The word "local" with an arrow points to the css-folder. Below, screenshot with heading "github", and a css-folder and index.php-file

最佳答案

(注意:starting Oct. 2020,任何新的存储库都是使用默认分支 main 创建的,而不是 master。您可以 rename existing repository default branch from master to main
2014 年答案的其余部分已更新为使用“main”)

(以下假设 github.com 本身没有关闭,正如 eri0othe comments 中指出的那样:请参阅 www.githubstatus.com 确定)

如果 GitHub 存储库看到新的提交推送到它,而你在本地工作,我建议使用:

git pull --rebase
git push

完整的语法是:

git pull --rebase origin main
git push origin main

With Git 2.6+ (2015年9月),完成后(一次)

git config --global pull.rebase true
git config --global rebase.autoStash true

一个简单的 git pull 就足够了。
(注意:使用 Git 2.27 Q2 2020 时,merge.autostash 也可用于您的常规 pull ,无需 rebase )

这样,您将在新更新的 origin/main(或 origin/yourBranch)之上重播(--rebase 部分)您的本地提交: git pull origin yourBranch).

请参阅 chapter 6 Pull with rebase 中的更完整示例的 Git Pocket Book .

我会推荐:

# add and commit first
#
git push -u origin main

# Or git 2.37 Q2 2022+
git config --global push.autoSetupRemote true
git push

这将在您的本地主分支与其上游分支之间建立跟踪关系。
之后,该分支的任何 future 推送都可以通过简单的方式完成:

git push

同样,Git 2.37+ and its global option push.autoSetupRemote ,一个简单的 git push 即使是第一个也会做同样的事情(即:在你的本地 main 分支和它的上游分支 origin/main 之间建立一个跟踪关系)。

参见“Why do I need to explicitly push a new branch?”。


由于 OP 已经 reset and redone its commitorigin/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

注意:git reset --mixed origin/main也可以写成git reset origin/main,因为--mixed选项是使用 git reset 时的默认值.

关于git 错误 : failed to push some refs to remote,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24114676/

相关文章:

git - Visual Studio - 远程 : HTTP Basic: Access denied Error encountered while cloning the remote repository: Git failed with a fatal error

git - 如何在不丢失任何一个存储库历史记录的情况下 merge 两个 git 存储库

git - Heroku 中出现 "Procfile declares types -> (none)"是什么原因?

github - 如何列出所有 GitHub 用户?

github - 有条件地触发 TravisCI 构建阶段?

git - 使用 merge 的提交重新设置Git

git - 当我尝试推送提交时,VS Code 说我的分支没有远程分支,该分支出现在 VS Code 上的分支列表中的原点上

git - 使用 git stash save 或 git commit 进行本地更改?

github - 有没有办法在 Github README.md 中表示目录树?

GitHub 推送错误 - 'git media update'