跳至主要内容

处理非快进错误

有时,Git 无法在不丢失提交的情况下将你的更改推送到远程仓库。当出现这种情况时,Git 会拒绝你的推送。

如果其他人在与你相同的分支上已经推送了更改,Git 将无法推送你的更改

$ git push origin main
> To https://github.com/USERNAME/REPOSITORY.git
>  ! [rejected]        main -> main (non-fast-forward)
> error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.git'
> To prevent you from losing history, non-fast-forward updates were rejected
> Merge the remote changes (e.g. 'git pull') before pushing again. See the
> 'Note about fast-forwards' section of 'git push --help' for details.

你可以通过 获取并合并 远程分支上的更改与本地所做的更改来解决此问题

$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work

或者,你可以直接使用 git pull 一次性执行这两个命令

$ git pull origin YOUR_BRANCH_NAME
# Grabs online updates and merges them with your local work
© . This site is unofficial and not affiliated with GitHub, Inc.