如何让 Git 使用我选择的编辑器进行提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2596805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:12:28  来源:igfitidea点击:

How do I make Git use the editor of my choice for commits?

gitvimemacseditorcommit-message

提问by brasskazoo

I would prefer to write my commit messages in Vim, but it is opening them in Emacs.

我更喜欢在 Vim 中编写我的提交消息,但它在 Emacs 中打开它们。

How do I configure Git to always use Vim? Note that I want to do this globally, not just for a single project.

如何配置 Git 以始终​​使用 Vim?请注意,我想在全局范围内执行此操作,而不仅仅是针对单个项目。

回答by digitaldreamer

If you want to set the editor onlyfor Git, do either (you don't need both):

如果你想设置的编辑对Git的,要么做(你不需要两者):

  • Set core.editorin your Git config: git config --global core.editor "vim"
  • Set the GIT_EDITORenvironment variable: export GIT_EDITOR=vim
  • core.editor在您的 Git 配置中设置:git config --global core.editor "vim"
  • 设置GIT_EDITOR环境变量:export GIT_EDITOR=vim


If you want to set the editor for Git and also other programs, set the standardized VISUALand EDITORenvironment variables*:

如果要为 Git和其他程序设置编辑器,请设置标准化VISUALEDITOR环境变量*:

export VISUAL=vim
export EDITOR="$VISUAL"

* Setting both is not necessarily needed, but some programs may not use the more-correct VISUAL. See VISUALvs. EDITOR.

* 不一定需要同时设置,但有些程序可能不使用更正确的VISUAL. 见VISUALEDITOR.



For Sublime Text: Add this to the .gitconfig. The --waitis important (it allows to type text in sublime and will wait for save/close event.)

对于 Sublime Text:将其添加到.gitconfig. 该--wait是很重要的(它允许在崇高键入文本并等待保存/关闭事件。)

[core]
    editor = 'subl' --wait

'subl' can be replaced by the full path of the executable but is usually available when correctly installed.

'subl' 可以替换为可执行文件的完整路径,但通常在正确安装时可用。

回答by Mark Rushakoff

Copy paste this:

复制粘贴这个:

git config --global core.editor "vim"

In case you'd like to know what you're doing. From man git-commit:

如果你想知道你在做什么。来自man git-commit

ENVIRONMENT AND CONFIGURATION VARIABLES

The editor used to edit the commit log message will be chosen from the GIT_EDITORenvironment variable, the core.editorconfiguration variable, the VISUALenvironment variable, or the EDITORenvironment variable (in that order).

环境和配置变量

用于编辑提交日志消息的编辑器将从GIT_EDITOR环境变量、core.editor配置变量、VISUAL环境变量或EDITOR环境变量(按此顺序)中选择。

回答by Koen.

On Ubuntu and also Debian (thanks @MichielB) changing the default editor is also possible by running:

在 Ubuntu 和 Debian(感谢 @MichielB)上,也可以通过运行以下命令来更改默认编辑器:

sudo update-alternatives --config editor

Which will prompt the following:

这将提示以下内容:

There are 4 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /bin/nano            40        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
* 3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.tiny    10        manual mode

Press enter to keep the current choice[*], or type selection number: 

回答by Anmol Saraf

In windows 7, while adding the "Sublime" editor it was still giving me an error:

在 Windows 7 中,在添加“Sublime”编辑器时,它仍然给我一个错误:

Aborting commit due to empty commit message.

由于空提交消息而中止提交。

Sublime was not able to keep the focus.

Sublime 无法保持焦点。

To fix this I opened the .gitconfig file in c:/users/username/ folder and added the following line with --wait option outside the double quotes.

为了解决这个问题,我在 c:/users/username/ 文件夹中打开了 .gitconfig 文件,并在双引号外添加了以下带有 --wait 选项的行。

[core]
      editor = 'F:/Program Files/Sublime Text 2/sublime_text.exe' --wait

Hope its helpful to somebody facing similar issue with Sublime.

希望它对面临与 Sublime 类似问题的人有所帮助。

回答by Ryan

In Windows 7, setting editor to Notepad++

在 Windows 7 中,将编辑器设置为 Notepad++

  • Open any text editor.
  • Open this file: C:\Users\YOUR_USERNAME\.gitconfig
  • Add this section to the bottom:
  • 打开任何文本编辑器。
  • 打开这个文件: C:\Users\YOUR_USERNAME\.gitconfig
  • 将此部分添加到底部:


[core]
    editor = 'C:/Program Files (x86)/Notepad++/notepad++.exe' 
  • Save and close the file.
  • When you're committing with git, just write git commitand press Enter. It will pop open Notepad++.
  • Write your commit message at the top of the file, and save and close the file. Done!
  • 保存并关闭文件。
  • 当您使用 git 提交时,只需编写git commit并按Enter。它将弹出打开记事本++。
  • 在文件顶部写下您的提交消息,然后保存并关闭文件。完毕!

回答by Marcelo Mason

To Make Visual Studio Code (vscode) your default git editor

使 Visual Studio Code (vscode) 成为您的默认 git 编辑器

git config --global core.editor "code --wait"

回答by tristanbailey

And if you are working with designers using the command line then Pico, and dont know short cuts ;)

如果您使用命令行与设计师合作,那么 Pico,并且不知道捷径;)

git config --global core.editor "pico"

Or

或者

export VISUAL=pico
export EDITOR=pico

回答by 2Toad

Atom as your git editor

Atom 作为您的 git 编辑器

git config --global core.editor "atom --wait"

Atom needs to be configured to run from the command line for the above to work:

OS X: install shell commands from Atom: menu bar > Atom > Install Shell Commands

Windows: no action required - atom is configured to run from the command line by default

Atom 需要配置为从命令行运行才能使上述工作正常运行:

OS X:从 Atom 安装 shell 命令:菜单栏 > Atom > 安装 Shell 命令

Windows:不需要任何操作 - 默认情况下,原子被配置为从命令行运行

回答by Mohsen

Setting Sublime Text 2 as Git commit editor in Mac OSX 10

在 Mac OSX 10 中将 Sublime Text 2 设置为 Git 提交编辑器

Run this command:

运行此命令:

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

Or just:

要不就:

$ git config --global core.editor "subl -w"

回答by Sна?ош?а?

Windows: setting notepad as the default commit message editor

Windows:将记事本设置为默认提交消息编辑器

git config --global core.editor notepad.exe

Hit Ctrl+Sto save your commit message. To discard, just close the notepad window without saving.

点击Ctrl+S以保存您的提交消息。要丢弃,只需关闭记事本窗口而不保存。

In case you hit the shortcut for save, then decide to abort, go to File->Save as, and in the dialog that opens, change "Save as type" to "All files (*.*)". You will see a file named "COMMIT_EDITMSG". Delete it, and close notepad window.

如果您点击了保存的快捷方式,然后决定中止,请转到文件->另存为,然后在打开的对话框中,将“保存类型”更改为“所有文件(*.*)”。您将看到一个名为“COMMIT_EDITMSG”的文件。删除它,并关闭记事本窗口。

Edit:Alternatively, and more easily, delete all the contents from the open notepad window and hit save. (thanks mwfearnleyfor the comment!)

编辑:或者,更轻松地,从打开的记事本窗口中删除所有内容并点击保存。(感谢mwfearnley的评论!)

I think for small write-ups such as commit messages notepad serves best, because it is simple, is there with windows, opens up in no time. Even your sublime may take a second or two to get fired up when you have a load of plugins and stuff.

我认为对于像提交消息这样的小文章,记事本最适合,因为它很简单,有窗口,很快就会打开。当你有大量的插件和东西时,即使你的 sublime 也可能需要一两秒钟才能启动。