如果其他人已将更改推送到与您相同的分支,则 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