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

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


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?请注意,我想在全球范围内完成这个任务,而不仅仅是一个项目。

18 个解决方案

#1


2556  

If you want to set the editor only for Git, do either (you don’t need both):

如果您只想为Git设置编辑器,那么可以(不需要两者):

  • Set core.editor in your Git config: git config --global core.editor "vim"
  • 设置的核心。Git配置中的编辑器:Git配置——全局核心。编辑“活力”
  • Set the GIT_EDITOR environment variable: export GIT_EDITOR=vim
  • 设置GIT_EDITOR环境变量:export GIT_EDITOR=vim。

If you want to set the editor for Git and also other programs, set the standardized VISUAL and EDITOR environment variables*:

如果您想为Git和其他程序设置编辑器,请设置标准化的可视化和编辑器环境变量*:

export VISUAL=vim
export EDITOR="$VISUAL"

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

*设置两者不一定是必要的,但是一些程序可能不会使用更正确的视觉。看到可视化和编辑器。


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

对于崇高的文本:将其添加到.gitconfig中。等待是很重要的。(它允许在崇高中输入文本,并等待保存/关闭事件。

[core]
    editor = 'subl' --wait

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

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

#2


440  

Copy paste this:

复制粘贴:

git config --global core.editor "vim"

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

如果你想知道你在做什么。从男人git-commit:

ENVIRONMENT AND CONFIGURATION VARIABLES

The editor used to edit the commit log message will be chosen from the GIT_EDITOR environment variable, the core.editor configuration variable, the VISUAL environment variable, or the EDITOR environment variable (in that order).

用于编辑提交日志消息的编辑器将从GIT_EDITOR环境变量(核心)中选择。编辑器配置变量、可视环境变量或编辑器环境变量(按此顺序)。

#3


138  

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: 

#4


53  

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

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

Aborting commit due to empty commit message.

由于空提交消息而导致提交失败。

Sublime was not able to keep the focus.

崇高不能保持专注。

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文件,并在双引号之外添加了下面一行。

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

Hope its helpful to somebody facing similar issue with Sublime.

希望它对那些面临类似问题的人有帮助。

#5


43  

In Windows 7, setting editor to Notepad++

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

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

#6


21  

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

#7


15  

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

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"

#8


15  

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

要使Visual Studio代码(vscode)成为默认的git编辑器。

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

#9


13  

Atom as your git editor

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

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

Atom需要配置为从上面的命令行运行:

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

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

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

Windows:不需要操作—atom被配置为默认从命令行运行。

#10


9  

For emacs users

emacs用户

.emacs:

. emacs:

(server-start)

shellrc:

shellrc:

export EDITOR=emacsclient

#11


9  

Windows: setting notepad as the default commit message editor

git config --global core.editor notepad.exe

Hit Ctrl+S to 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.

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

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

编辑:或者更容易地,从打开的记事本窗口中删除所有内容并点击save。(感谢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.

我认为对于小的写操作,比如提交消息,记事本最好,因为它很简单,是有窗口的,没有时间打开。当你有大量的插件时,即使是你的崇高也可能需要一两秒的时间。

#12


4  

Best settings for Sublime Text 3 as your git editor (Windows & Linux instructions):

To follow these instructions in Windows make sure you have installed git for Windows. In Windows, I like to use Git BASH so that it feels more like Linux.

要遵循Windows中的这些说明,请确保已经为Windows安装了git。在Windows中,我喜欢使用Git BASH,让它感觉更像Linux。

First, we want to create a special Sublime Text project so that we can specify special project settings we want set whenever git calls the editor, to make things easier when editing in git. For example, I normally set my ruler to 120 chars in most projects, but for git commit messages I want it to be 72 chars so that it fits nicely in a terminal when you call git log or git lg.

首先,我们希望创建一个特殊的卓越的文本项目,这样我们就可以指定在git调用编辑器时需要设置的特殊项目设置,以便在git中编辑时更方便。例如,在大多数项目中,我通常将标尺设置为120个字符,但是对于git提交的消息,我希望它是72个字符,这样当你调用git log或git lg时,它就能很好地适合于一个终端。


1. Create a Sublime Text project with settings we want to use to edit git commit messages

Open Sublime Text and go to "File" --> "New Window" to create a new anonymous project. Go to "Project" --> "Save Project As..." and choose a place to save it. In Linux I saved it in my Linux home directory with the file name .gitconfig.sublime-project. It's path is therefore: ~/.gitconfig.sublime-project. In Windows also save it in your home directory, ex: C:\Users\MY_USER_NAME\.gitconfig.sublime-project Now go to "Project" --> "Edit Project" to edit the project settings. Paste the following and save the settings. Make any further edits for your project settings if desired.

打开崇高的文本,进入“文件”——>“新窗口”,创建一个新的匿名项目。进入“项目”——>“保存项目为…”,并选择一个地方保存它。在Linux中,我将它保存在我的Linux主目录中,文件名是.gitconfig.sublime-project。因此,它的路径是:~/.gitconfig.sublime-project。在Windows中也将其保存在您的主目录,例:C:\Users\ MY_USER_NAME \ .gitconfig。sublime-project现在转到“Project”——>“编辑项目”来编辑项目设置。粘贴以下内容并保存设置。如果需要,为您的项目设置做进一步的编辑。

{
    //For folder settings help see here: https://www.sublimetext.com/docs/3/projects.html 

    "folders":
    [

    ],

    "settings":
    {

        // Disables horizontal scrolling if enabled.
        // May be set to true, false, or "auto", where it will be disabled for
        // source code, and otherwise enabled.
        "word_wrap": false,

        // Set to a value other than 0 to force wrapping at that column rather than the
        // window width
        "wrap_width": 0,

        // Columns in which to display vertical rulers
        "rulers": [72, 50], //72 is recommended by git for commit message content, and 50 for commit titles

        // The number of spaces a tab is considered equal to
        "tab_size": 4,

        // Set to true to insert spaces when tab is pressed
        "translate_tabs_to_spaces": true,
    },

    "build_systems":
    [

    ]

}

2. Set the editor to be used by git

Now we need to set the editor to be used by git, by editing the .gitconfig file.

现在我们需要通过编辑.gitconfig文件将编辑器设置为git。

For Linux:

Your user copy of this will be located in ~/.gitconfig. Open this file and add the following lines. Be sure to use the proper path name to the git project you just created above! I'm using ~/.gitconfig.sublime-project.

您的用户副本将位于~/.gitconfig中。打开此文件并添加以下代码行。一定要使用您刚刚创建的git项目的正确路径名!我用~ / .gitconfig.sublime-project。

[core]
    editor = subl --project ~/.gitconfig.sublime-project --wait

The --wait is important, as it forces git to wait until you close the file before it continues on. The --project line is important to tell Sublime Text which project you want opened whenever git opens Sublime.

等待是很重要的,因为它迫使git等待在文件继续之前关闭文件。当git打开时,项目行很重要,它可以告诉你想要打开哪个项目。

Per @digitaldreamer's answer above (https://stackoverflow.com/a/2596835/4561887), "subl can be replaced by the full path of the executable but [the alias subl] is usually available when [Sublime is] correctly installed."

每个@digitaldreamer的答案(https://stackoverflow.com/a/2596835/4561887),“subl可以被可执行文件的完整路径替换,但是,当[Sublime]正确安装时,通常可以使用[别名subl]。”

For Windows:

For Windows, first read the Linux instructions for background info. Now we will do something almost identical.

对于Windows,首先读取背景信息的Linux指令。现在我们要做一些几乎相同的事情。

(OPTIONAL: create a subl alias for use in Git BASH):

(可选:为Git BASH创建一个subl别名):

Open up a text editor (ex: Notepad, Notepad++, Sublime Text, Geany, etc), and create a file called ".bash_profile" in your home directory. Its path will therefore be: C:\Users\MY_USER_NAME\.bash_profile. Save the following into it:

打开一个文本编辑器(例如:记事本、Notepad++、卓越的文本、Geany等),并创建一个名为“的文件”。bash_profile“在您的主目录中。因此它的路径是:C:\Users\ MY_USER_NAME \ . bash_profile。保存以下内容:

alias subl="/c/Program\ Files/Sublime\ Text\ 3/subl.exe"

This creates a Git BASH alias called subl that we can now use in Git BASH for Windows, to easily open Sublime. This step isn't required but it's useful for general Git BASH use. Now you can call subl ., for instance, in Git BASH to open up a new Sublime Project in your current directory.

这就创建了一个名为subl的Git BASH别名,我们现在可以在Git BASH中使用它来打开Windows,以方便地打开“Sublime”。这个步骤不是必需的,但是它对Git BASH使用非常有用。现在您可以调用subl,例如,在Git BASH中打开当前目录中的一个新的崇高项目。

(MANDATORY):

(强制):

Edit the .gitconfig file found in your home directory: C:\Users\MY_USER_NAME\.gitconfig, by adding the following to it. Notice the subtle changes from the Linux instructions above:

编辑.gitconfig文件位于您的主目录:C:\Users\ MY_USER_NAME \。gitconfig,通过添加下面的内容。请注意上面的Linux说明的细微变化:

[core]
  editor = 'C:/Program Files/Sublime Text 3/subl.exe' --project ~/.gitconfig.sublime-project --wait
  • Notice that you must specify the full path to the Sublime Text executable. Note the direction of the slashes! Use / NOT \ to separate folders in the path name! (Thanks VonC for helping me see this).
  • 请注意,您必须指定可执行文本的完整路径。注意斜杠的方向!使用/不\在路径名中分隔文件夹!(谢谢VonC帮我看这个)。
  • Our subl alias we made for Git BASH above doesn't work here, so you can't use it like we did in the Linux example, instead you must specify the whole path as shown above.
  • 我们为Git BASH创建的subl别名在这里不起作用,因此您不能像在Linux示例中那样使用它,而是必须指定上面所示的整个路径。
  • The ~ symbol, however, does still work here to get to your Windows home directory.
  • 但是,~符号仍然在这里工作以到达您的Windows主目录。

2.5. (Optional) Install the "Git" package into Sublime Text 3.

This gives you syntax highlighting for git commit messages, as well as access to other git commands such as git blame (which I use frequently in Sublime Text) or git commit (which I don't use in Sublime Text since I'd prefer the command-line for general git flow, as I've mentioned in my comments below this answer).

这对于git提交信息给你语法高亮,以及访问其他git命令如git指责(我经常使用崇高文本)或git commit(我不用在崇高的文本,因为我宁愿git命令行一般流程,如下我的评论中我提到过这个答案)。

To install a package: First, ensure “Package Control” is installed. Next, press Ctrl + Shift + P (same as Tools → Command Palette) and type all or part of “Package Control: Install Package”, then press Enter. In the search box that comes up, search for the package "Git" and hit Enter on it, or click on it, to automatically install it.

安装包:首先,确保安装了“包控制”。接下来,按Ctrl + Shift + P(一样工具→命令面板)和类型的全部或部分包控制:安装包,然后按Enter键。在搜索框中,搜索包“Git”,点击进入它,或者点击它,自动安装它。

Once installed, Ctrl + Shift + P then searching for "git" will bring up git commands you can use internally inside Sublime Text now, such as git blame.

一旦安装了,Ctrl + Shift + P然后搜索“git”就会弹出git命令,你可以在内部使用它,比如git。


3. Use it

Now when you call git commit, for instance, as normal from the command-line, Sublime Text will open up into the .gitconfig.sublime-project we created above, with that project's settings! As you type a paragraph you'll notice it extends past the ruler we set since soft word-wrap is off. To force hard wrap via auto-inserted hard-returns at the end of each line, put your cursor on the long line you want auto-wrapped and press Alt + Q. It will now hard-wrap/hard-fold at 72 chars, which is what we set in the project settings' "rulers" parameter above.

现在,当您调用git提交时,例如,从命令行开始,将会打开。gitconfig。我们在上面创建的sublime-project,有这个项目的设置!在键入一段你会注意到它延伸过去的统治者我们组由于软自动换行了。努力力包装通过自动hard-returns结束时,每一行,把你的鼠标放在你想要的长队auto-wrapped并按Alt +问:现在hard-wrap / hard-fold 72字符,这是我们在项目设置中设置的“统治者”参数。

Now, save your commit message with Ctrl + S, and exit (to complete your git commit) with Ctrl + Shift + W.

现在,用Ctrl + S保存您的提交消息,并使用Ctrl + Shift + W退出(以完成您的git提交)。

Done!

完成了!

#13


2  

Mvim as your git editor

Like all the other GUI applications, you have to launch mvim with the wait flag.

与所有其他GUI应用程序一样,您必须使用等待标志启动mvim。

git config --global core.editor "mvim --remote-wait"

#14


1  

Just try EDITOR=vim git commit.

尝试编辑器=vim git提交。

Or you can set your EDITOR to vim by export EDITOR=vim in your bashrc.

或者,您可以在bashrc中通过export EDITOR=vim将编辑器设置为vim。

#15


1  

For users of TextWrangler from the Mac app store:

git config --global core.editor "open -n -W -a TextWrangler"

Also, make sure your "TextWrangler > Preferences > Application > When TextWrangler becomes active:" setting is set to "Do nothing"

同时,确保你的“TextWrangler >偏好>应用>当TextWrangler变得活跃时”设置为“什么都不做”。

This works for me on OS X 10.11.4 with TextWrangler 5.0.2 from the Mac app store.

这对我来说是在OS X 10.11.4上,在Mac应用商店中使用TextWrangler 5.0.2。

Explanation:

解释:

The -n means open in a new instance.

n表示在一个新实例中打开。

The -W means to wait until the application exits before using the contents of the edited file as the commit message.

w的意思是等待应用程序退出,然后使用编辑的文件的内容作为提交消息。

The -a TextWrangler means use the TextWrangler application to open the file.

一个TextWrangler意味着使用TextWrangler应用程序打开文件。

See man open in your Mac Terminal app for more details.

在你的Mac终端应用中,可以看到man打开的更多细节。

#16


1  

For Windows users who want to use neovim with the Windows Subsystem for Linux:

对于想要使用新vim的Windows用户来说,Linux的Windows子系统:

git config core.editor "C:/Windows/system32/bash.exe --login -c 'nvim .git/COMMIT_EDITMSG'"

git配置核心。编辑“C:/窗/ system32系统/ bash。exe -login -c 'nvim .git/COMMIT_EDITMSG'"

This is not a fool-proof solution as it doesn't handle interactive rebasing (for example). Improvements very welcome!

这不是一个简单的解决方案,因为它不能处理交互式的rebase(例如)。改进非常欢迎!

#17


-1  

For Textmate Users

This opens Textmate editor in when you want to edit your commits. Requires textmate command line tools to be installed.

当您想要编辑提交时,这将打开Textmate编辑器。需要安装textmate命令行工具。

git config --global core.editor "mate -w"

git配置——全球核心。编辑“伴侣- w”

#18


-1  

For Windows users who want to use Kinesics Text Editor

Create a file called 'k.sh', add the following text and place in your home directory (~):

创建一个名为“k”的文件。在您的主目录(~)中添加以下文本和位置:

winpty "C:\Program Files (x86)\Kinesics Text Editor\x64\k.exe" $1

At the git prompt type:

在git提示类型:

git config --global core.editor ~/k.sh
关注微信公众号

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/04/08/b0feb55c455899cd47cd131ce2d0f77f.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com