我正在试用Windows上的Git。我到了尝试“git commit”的地步,我得到了这个错误:

终端是哑的,但没有视觉也 编辑器定义。请提供 消息使用-m或-F选项。

所以我发现我需要一个叫做EDITOR的环境变量。没有问题。我把它设置为指向记事本。这几乎奏效了。默认的提交消息在记事本中打开。但是记事本不支持换行。我出去得到了notepad++,但我不知道如何将notepad++设置为%EDITOR%,使其与Git正常工作。

我没有嫁给notepad++。在这一点上,我不介意我使用什么编辑器。我只是希望能够在编辑器中而不是在命令行中输入提交消息(使用-m)。

那些在Windows上使用Git的人:你使用什么工具来编辑你的提交消息,你必须做什么才能让它工作?


当前回答

根据VonC的建议,这对我很有效(差点把我逼疯了):

git config --global core.editor "'C:/Program Files (x86)/Sublime Text 3/subl.exe' -wait"

省略-wait可能会导致问题,特别是如果你正在使用Gerrit,并且更改id必须手动复制到提交消息的底部。

其他回答

如果路径中有空格,Git似乎无法找到编辑器。因此,您必须将Patrick回答中提到的批处理文件放到非空白路径中。

对于Atom,你可以这样做

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

Visual Studio Code也类似

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

它会打开一个Atom或Visual Studio Code窗口供你提交,

或Sublime Text:

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

根据Darren的回答,要使用notepad++,你可以简单地这样做(都在一行上):

git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"

显然,C:/Program Files/ notepad++ /notepad++.exe部分应该是系统上notepad++可执行文件的路径。例如,可能是C:/Program Files (x86)/ notepad++ /notepad++.exe。

这对我来说很有魔力。

文章如何将notepad++设置为默认的Git提交编辑器而不是Vim解释了该命令的参数。

假设你想配置VsCode为你的编辑器。 做以下几点:

在.gitconfig文件中添加以下代码行:

gitconfig文件的默认位置是C:\Users\USER_NAME\.gitconfig

[core]
  editor = code -w -n
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = code -w -n --diff $LOCAL $REMOTE
[merge]
  tool = vscode
[mergetool "vscode"]
  cmd = code -w -n $MERGED

注意: -w是必选项,告诉git等待vscode加载。 -n是可选的,它告诉git在新窗口中打开vscode。

如果你想在Windows中配置一个自定义的编辑器路径:

您需要将字code替换为VsCode的“。exe”路径。

例如:

[core]
  editor = "'C:/Users/Tal/AppData/Local/Programs/Microsoft VS Code/Code.exe'" -w -n
[diff]
  tool = vscode
[difftool "vscode"]
  cmd = "'C:/Users/Tal/AppData/Local/Programs/Microsoft VS Code/Code.exe'" -w -n --diff $LOCAL $REMOTE
[merge]
  tool = vscode
[mergetool "vscode"]
  cmd = "'C:/Users/Tal/AppData/Local/Programs/Microsoft VS Code/Code.exe'" -w -n $MERGED

注意: 你需要用单引号包围路径”。 路径中的斜杠应该是正斜杠/。

再举一个例子:

[core]
    editor = \"C:\\Users\\Tal\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" -w -n

[diff]
    tool = vscode
[difftool "vscode"]
    cmd = \"C:\\Users\\Tal\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" -w -n --diff $LOCAL $REMOTE

[merge]
    tool = vscode
[mergetool "vscode"]
    cmd = \"C:\\Users\\Tal\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" -w -n $MERGED

更新:

VsCode现在支持“3路合并”! 更新版本为1.69和1.70.0。 所以现在你可以启用VsCode的“mergetool”来查看3向合并。

为此,你需要更新行:

[mergetool "vscode"]
      cmd = code -w -n $MERGED

与新行:

[mergetool "vscode"]
      cmd = code -w -n --merge $REMOTE $LOCAL $BASE $MERGED

当使用远程挂载的主驱动器(Samba共享、NFS,…)时,您的~/。Git文件夹在所有系统中共享,这可能会导致几个问题。因此,我更喜欢用脚本来确定正确系统的正确编辑器:

#!/usr/bin/perl
# Detect which system I'm on and choose the right editor
$unamea = `uname -a`;
if($unamea =~ /mingw/i){
    if($unamea =~ /devsystem/i){#Check hostname
        exec('C:\Program Files (x86)\Notepad++\notepad++.exe', '-multiInst', '-nosession', @ARGV);
    }
    if($unamea =~ /testsystem/i){
        exec('C:\Program Files\Notepad++\notepad++.exe', '-multiInst', '-nosession', @ARGV);
    }
}
$MCEDIT=`which mcedit`;
if($MCEDIT =~ /mcedit/){
    exec($MCEDIT, @ARGV);
}
$NANO=`which nano`;
if($NANO =~ /nano/){
    exec($NANO, @ARGV);
}
die "You don't have a suitable editor!\n";

人们可能会考虑使用普通的shell脚本,但我使用的是Perl,因为它随msysgit一起提供,而且您的类unix系统通常也会提供一个。 将脚本放在/home/username/bin中,该脚本应该添加到.bashrc或.profile中的PATH中。一旦添加了git配置-全局核心。编辑器giteditor。pl你有正确的编辑器,无论你在哪里。