如何更换 Git 默认的提交信息编辑器
更换 Git 默认编辑器:git config --global core.editor,如 VS Code 用 "code --wait",nano 直接写 nano,Vim 写 vim。--wait 类参数保证 Git 等编辑器关闭后再继续。
设置 core.editor 配置项:git config --global core.editor "code --wait"。关键点是编辑器必须以"等待关闭"模式启动(GUI 编辑器的 --wait 参数),否则 Git 认为你瞬间编辑完毕,可能在修改保存前读取已有信息或因空消息中止。
常用编辑器配置
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "subl -w" # Sublime Text
git config --global core.editor "nano" # nano
git config --global core.editor "vim" # Vim
git config --global core.editor "notepad" # Windows 记事本
什么时候会触发编辑器
git commit(不带 -m);git commit --amend;git rebase -i的提交列表编辑;git merge产生合并提交时。
不想每次都弹编辑器,提交信息用 git commit -m "信息" 直接给。
交互 rebase 的 todo 编辑优先使用 GIT_SEQUENCE_EDITOR 或 sequence.editor,再退回普通编辑器;它与 commit message 编辑器可以不同。GUI 等待参数依编辑器而异,不是都叫 --wait。
系统级备选
Git 按 GIT_EDITOR 环境变量 → core.editor → VISUAL → EDITOR 的顺序找编辑器。shell 里设 export EDITOR=vim 对大多数命令行工具都生效。
常见问题(FAQ)
Q:配了 VS Code 但报错 "code: command not found"?
VS Code 的命令行入口没装:macOS 可在 VS Code 命令面板运行 Shell Command: Install code command in PATH;Windows/Linux 应按安装器和各平台 PATH 配置核对。
Q:Vim 里怎么保存退出?
Esc 然后 :wq 回车。想放弃这次提交::cq(非零退出码,Git 会中止提交)。
Q:只对当前项目换编辑器?
去掉 --global,在仓库内执行 git config core.editor ...,写入该仓库的 .git/config。