推送Unity项目至GitHub私有仓库遇non-fast-forward错误求助
解决Git推送Unity项目至GitHub私有仓库的non-fast-forward错误
你执行的终端命令:
git add . git commit -m "commit" git remote add origin "myrepolink" git push -u origin "first_part"
遇到的错误信息:
! [rejected] first_part -> first_part (non-fast-forward)
error: failed to push some refs to 'myrepolink'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
原因
远程分支first_part存在本地没有的提交记录,本地分支落后于远程分支,导致推送被拒绝。
解决方案
方案1:合并远程内容(推荐,安全不丢失数据)
- 拉取远程分支的最新内容并合并到本地:
git pull origin first_part - 如果拉取时出现冲突(比如Unity的场景文件、.meta文件冲突),手动打开冲突文件解决矛盾,之后执行:
git add . git commit -m "解决远程分支合并冲突" - 再次推送:
git push -u origin first_part
方案2:强制覆盖远程分支(谨慎使用,会丢失远程现有内容)
如果你确认远程分支的内容不需要保留,可以强制推送覆盖:
git push -u origin first_part --force
Unity项目额外注意事项
- 确保配置了正确的
.gitignore文件,排除Library、Temp、obj等Unity自动生成的文件夹,减少不必要的文件冲突。 - 推送前用
git status检查待提交文件,避免误提交冗余内容。
内容的提问来源于stack exchange,提问作者Sebastian Filipovici
相关产品推荐
相关产品推荐

