如何更换 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 "信息" 直接给。
系统级备选
Git 按 core.editor → GIT_EDITOR 环境变量 → VISUAL → EDITOR 的顺序找编辑器。shell 里设 export EDITOR=vim 对大多数命令行工具都生效。
常见问题(FAQ)
Q:配了 VS Code 但报错 "code: command not found"?
VS Code 的命令行入口没装:VS Code 里 Cmd/Ctrl+Shift+P → 运行 "Shell Command: Install 'code' command in PATH"。
Q:Vim 里怎么保存退出?
Esc 然后 :wq 回车。想放弃这次提交::cq(非零退出码,Git 会中止提交)。
Q:只对当前项目换编辑器?
去掉 --global,在仓库内执行 git config core.editor ...,写入该仓库的 .git/config。