Git推送失败提示拉取但拉取也报错,如何将本地内容推送到远程仓库?
Git Push报错及Pull分支冲突问题解决
问题场景
执行git push时出现以下错误:
sudhakar@featuriz-ubuntu:~/NB/2023/pro$ git push Password for 'https://featuriz@github.com': To https://github.com/featuriz/pro.git ! [rejected] development -> development (non-fast-forward) error: failed to push some refs to 'https://github.com/featuriz/pro.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.
按照提示执行git pull时,又出现分支冲突提示:
sudhakar@featuriz-ubuntu:~/NB/2023/pro$ git pull Password for 'https://featuriz@github.com': From https://github.com/featuriz/pro * [new branch] Modified -> origin/Modified hint: You have divergent branches and need to specify how to reconcile them. hint: You can do so by running one of the following commands sometime before hint: your next pull: hint: hint: git config pull.rebase false # merge (the default strategy) hint: git config pull.rebase true # rebase hint: git config pull.ff only # fast-forward only hint: hint: You can replace "git config" with "git config --global" to set a default hint: preference for all repositories. You can also pass --rebase, --no-rebase, hint: or --ff-only on the command line to override the configured default per hint: invocation. fatal: Need to specify how to reconcile divergent branches.
需求说明:需要将本地内容推送到GitHub远程仓库(local -> remote),且不敢随意执行Git命令,寻求安全可行的解决方案。
Git文档参考:
git pull 命令用于从远程仓库获取并下载内容,
同时立即更新本地仓库以匹配这些内容。
在基于Git的协作工作流中,将远程上游变更合并到本地仓库是一项常见任务。
安全解决方案
1. 先备份本地仓库(关键)
复制整个本地仓库文件夹到其他位置,确保本地修改不会因操作失误丢失。
2. 选择合适的分支合并策略
以下两种是常用的安全处理方式,可根据需求选择:
方式一:Merge合并(保留完整提交历史)
若希望保留远程与本地的所有提交记录,执行:
# 临时指定本次pull使用merge策略(仅当前仓库生效) git pull --no-rebase
执行后Git会自动合并远程分支到本地,若出现代码冲突,手动解决冲突后执行:
git add . git commit -m "合并远程development分支变更" git push
方式二:Rebase变基(提交历史更线性)
若希望提交历史简洁连贯,执行:
# 临时指定本次pull使用rebase策略(仅当前仓库生效) git pull --rebase
若出现冲突,解决冲突后执行:
git add . git rebase --continue # 若需放弃rebase,执行 git rebase --abort
完成变基后执行git push即可。
3. (可选)设置默认pull策略
若不想每次pull都指定策略,可设置当前仓库的默认规则:
# 设置为merge(默认策略) git config pull.rebase false # 或设置为rebase git config pull.rebase true
若要让规则对所有本地仓库生效,添加--global参数:
git config --global pull.rebase true
内容的提问来源于stack exchange,提问作者Sudhakar Krishnan
相关产品推荐
相关产品推荐

