fatal: Not possible to fast-forward, aborting.
Git 报错警告问题
一、常见报错
在 VS Code 点击“拉取”或“同步更改”时,可能出现:
fatal: Not possible to fast-forward, aborting.
通常对应的拉取命令是:
git pull --tags origin master
二、问题原因
当前项目可能配置了:
pull.ff=only
该配置表示只允许“快进拉取”。当本地和远端分别存在不同提交时,分支会产生分叉:
远端:A - B - C
本地:A - D
本地存在提交 D,远端存在提交 B、C,Git 无法直接快进,因此普通 git pull 会失败。
这不是 VS Code 故障,而是 Git 拉取策略导致的。
三、临时解决办法
需要保留本地提交时执行:
git pull --rebase --tags origin master
如果项目使用 main 分支:
git pull --rebase --tags origin main
该命令会先取得远端提交,再把本地提交重新放到远端最新提交之后。
四、单个项目永久配置
进入项目目录后执行:
git config --local --unset pull.ff
git config --local pull.rebase true
如果第一条命令提示找不到配置,可以忽略,继续执行第二条。
检查配置:
git config --show-origin --get pull.ff
git config --show-origin --get pull.rebase
预期结果:
pull.ff没有输出。pull.rebase显示true。
五、所有项目全局配置
希望以后所有新项目默认使用 rebase,可以执行:
git config --global --unset pull.ff
git config --global pull.rebase true
已有项目如果在自己的 .git/config 中配置了 pull.ff=only,全局配置不会覆盖它。需要在对应项目中额外执行:
git config --local --unset pull.ff
查看配置及其来源:
git config --show-origin --get-all pull.ff
git config --show-origin --get-all pull.rebase
六、VS Code 配置
打开 VS Code 设置,搜索:
Git: Rebase When Sync
将该选项勾选。
也可以在 VS Code 的 settings.json 中添加:
{
"git.rebaseWhenSync": true
}
七、发生冲突时
查看冲突文件:
git status
解决冲突后执行:
git add 冲突文件路径
git rebase --continue
如果要放弃本次 rebase:
git rebase --abort
八、拉取前检查
拉取前先确认工作区状态:
git status
如果存在未提交修改,建议先提交,或者临时暂存:
git stash
git pull --rebase
git stash pop
九、推荐开发方式
尽量不要直接在 master 或 main 分支开发。推荐先更新主分支,再创建功能分支:
git switch master
git pull --rebase
git switch -c 功能分支名称
功能开发完成后,通过合并请求合入主分支,可以减少多人开发时频繁出现分支分叉的问题。
本文来自博客园,作者:jialiangzai,转载请注明原文链接:https://www.cnblogs.com/zsnhweb/p/22289843

浙公网安备 33010602011771号