如何让Git使用指定的编辑器编辑提交信息

如何让Git使用指定的编辑器编辑提交信息

技术背景

在使用Git进行版本控制时,每次提交代码都需要编写提交信息。默认情况下,Git会使用系统自带的编辑器(如vivim)来让用户输入提交信息。然而,不同用户可能有自己习惯的编辑器,如Sublime Text、VS Code等。因此,需要配置Git以使用指定的编辑器来编辑提交信息。

实现步骤

设置Git的默认编辑器

可以通过以下两种方式设置Git的默认编辑器:

  1. 修改Git配置:使用git config命令修改core.editor配置项。例如,将默认编辑器设置为vim
1
git config --global core.editor "vim"
  1. 设置环境变量:设置GIT_EDITOR环境变量。例如:
1
export GIT_EDITOR=vim

设置所有程序的默认编辑器

设置标准化的VISUALEDITOR环境变量:

1
2
export VISUAL=vim
export EDITOR="$VISUAL"

虽然设置两者不是必需的,但有些程序可能不会使用更正确的VISUAL

解决兼容性问题

有些编辑器需要--wait标志,否则会打开一个空白页面。例如:

  • Sublime Text
1
export VISUAL="subl --wait"
  • VS Code
1
export VISUAL="code --wait"

不同系统和编辑器的配置示例

Ubuntu和Debian

可以通过以下命令更改默认编辑器:

1
sudo update-alternatives --config editor

Windows 7

  • Notepad++
    • 打开C:\Users\YOUR_USERNAME\.gitconfig文件。
    • 对于64位Notepad++,添加以下内容:
1
2
[core]
editor = 'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar
- 对于32位Notepad++,添加以下内容:
1
2
[core]
editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' -multiInst -notabbar
  • Sublime Text:在.gitconfig文件中添加以下内容:
1
2
[core]
editor = 'F:/Program Files/Sublime Text 2/sublime_text.exe' --wait
  • VS Code
1
git config --global core.editor "code --wait"
  • Notepad
1
git config --global core.editor notepad.exe

Mac OSX

  • Sublime Text 2
1
git config --global core.editor "/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl"

1
git config --global core.editor "subl -w"
  • TextEdit
1
git config --global core.editor "open -W -n"

使用Sublime Text 3或4作为Git编辑器

  1. 创建Sublime Text项目:打开Sublime Text,创建一个新的匿名项目,保存为.gitconfig.sublime-project,并编辑项目设置:
1
2
3
4
5
6
7
8
9
10
11
{
"folders": [],
"settings": {
"word_wrap": false,
"wrap_width": 0,
"rulers": [72, 50],
"tab_size": 4,
"translate_tabs_to_spaces": true
},
"build_systems": []
}
  1. 设置Git使用的编辑器
    • Linux:在~/.gitconfig文件中添加以下内容:
1
2
[core]
editor = subl --project ~/.gitconfig.sublime-project --wait
- **Windows**:
    - (可选)在`.bashrc`或`.bash_profile`文件中创建`subl`别名:
1
alias subl="/c/Program\ Files/Sublime\ Text/subl.exe"
    - 在`C:\Users\MY_USER_NAME\.gitconfig`文件中添加以下内容:
1
2
[core]
editor = 'C:/Program Files/Sublime Text/subl.exe' --project ~/.gitconfig.sublime-project --wait
  1. (可选)安装“Git”包:在Sublime Text中安装“Git”包,以获得语法高亮和其他Git命令支持。

恢复默认编辑器

如果想恢复Git的默认编辑器,可以使用以下命令:

1
git config --global --unset core.editor

核心代码

设置Git默认编辑器为vim

1
git config --global core.editor "vim"

设置GIT_EDITOR环境变量

1
export GIT_EDITOR=vim

设置Sublime Text为Git编辑器(Windows)

1
2
[core]
editor = 'C:/Program Files/Sublime Text/subl.exe' --project ~/.gitconfig.sublime-project --wait

最佳实践

  • 在设置编辑器时,尽量使用--wait标志,确保Git在编辑器关闭后继续执行。
  • 对于不同的操作系统和编辑器,参考相应的配置示例进行设置。
  • 如果需要在多个项目中使用不同的编辑器,可以在项目的.git/config文件中进行局部配置。

常见问题

  • 编辑器打开空白页面:有些编辑器需要--wait标志,确保Git等待编辑器关闭后再继续执行。
  • Sublime Text无法保持焦点:在.gitconfig文件中添加--wait选项。
  • 恢复默认编辑器:使用git config --global --unset core.editor命令。

如何让Git使用指定的编辑器编辑提交信息
https://119291.xyz/posts/2025-05-09.how-to-set-git-editor-for-commit-messages/
作者
ww
发布于
2025年5月9日
许可协议