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

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

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

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

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


当前回答

假设你想配置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

其他回答

这适用于PowerShell和cmder 1.2(当与PowerShell一起使用时)。在文件~/.gitconfig中:

[core]
    editor = 'c:/program files/sublime text 3/subl.exe' -w

如何使Sublime Text成为Git的默认编辑器?

我只是遇到了同样的问题,却找到了不同的解决方案。我正在

error: There was a problem with the editor 'ec'

我有VISUAL=ec,在我的路径上有一个名为ec.bat的批处理文件,其中包含一行:

c:\emacs\emacs-23.1\bin\emacsclient.exe %*

这让我可以用ec <filename>从命令行编辑文件,并且拥有VISUAL set意味着大多数unix程序也会选择它。Git搜索路径的方式似乎与我的其他命令不同——当我在进程监视器中查看Git提交时,我看到它在ec和ec.exe路径上的每个文件夹中查找,但没有ec.bat。我添加了另一个环境变量(GIT_EDITOR=ec.bat),一切正常。

2015年9月更新(6年后)

git-for-Windows的最新版本(2.5.3)现在包括:

通过配置git配置核心。编辑notepad,用户现在可以使用notepad.exe作为默认编辑器。 commitmessagecolumns 72将被记事本包装器拾取,并在用户编辑后对提交消息进行换行。

参见Johannes Schindelin (dscho)的commit 69b301b。

Git 2.16(2018年第一季度)在生成编辑器时将显示一条消息告诉用户它正在等待用户完成编辑,以防编辑器 打开到一个隐藏的窗口或某个模糊的地方,用户得到 丢失。

参见Lars Schneider (larsxschneider)提交abfb04d(2017年12月7日)和提交a64f213(2017年11月29日)。 协助:Junio C Hamano(吉特)。 (由Junio C Hamano—gitster—在commit 0c69a13中合并,2017年12月19日)

launch_editor(): indicate that Git waits for user input When a graphical GIT_EDITOR is spawned by a Git command that opens and waits for user input (e.g. "git rebase -i"), then the editor window might be obscured by other windows. The user might be left staring at the original Git terminal window without even realizing that s/he needs to interact with another window before Git can proceed. To this user Git appears hanging. Print a message that Git is waiting for editor input in the original terminal and get rid of it when the editor returns, if the terminal supports erasing the last line


原来的答案

我刚刚用git版本1.6.2.msysgit.0.186测试了它。gf7512和notepad++ 5.3.1

我更喜欢不设置编辑器变量,所以我尝试了:

git config --global core.editor "\"c:\Program Files\Notepad++\notepad++.exe\""
# or
git config --global core.editor "\"c:\Program Files\Notepad++\notepad++.exe\" %*"

它总是给出:

C:\prog\git>git config --global --edit
"c:\Program Files\Notepad++\notepad++.exe" %*: c:\Program Files\Notepad++\notepad++.exe: command not found
error: There was a problem with the editor '"c:\Program Files\Notepad++\notepad++.exe" %*'.

如果我定义一个npp.bat,包括:

"c:\Program Files\Notepad++\notepad++.exe" %*

然后我输入:

C:\prog\git>git config --global core.editor C:\prog\git\npp.bat

它只能在DOS会话中工作,而不能在git shell中工作。 (不是那个核心。编辑器配置机制,一个脚本“start /WAIT…”在它不会工作,但只打开一个新的DOS窗口)


Bennett的回答提到了避免添加脚本的可能性,而是在简单的引号之间直接引用程序本身。注意斜线的方向!使用/ NOT \以路径名分隔文件夹!

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

如果你使用的是64位系统:

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

但是我更喜欢使用脚本(见下文):这样我就可以使用不同的路径或不同的选项,而不必再次注册git配置。


实际的解决方案(使用脚本)是实现: 你在配置文件中提到的实际上是一个shell (/bin/sh)脚本,而不是DOS脚本。

所以有效的方法是:

C:\prog\git>git config --global core.editor C:/prog/git/npp.bat

C: /食物/ git / npp.bat:

#!/bin/sh
"c:/Program Files/Notepad++/notepad++.exe" -multiInst "$*"

or

#!/bin/sh
"c:/Program Files/Notepad++/notepad++.exe" -multiInst -notabbar -nosession -noPlugin "$*"

有了这个设置,我可以从DOS或git Shell执行'git config -global -edit',或者我可以执行'git rebase -i…从DOS或Git Shell。 Bot命令将触发一个notepad++的新实例(因此使用-multiInst'选项),并等待该实例关闭后再继续。

注意,我只使用'/',而不是\'。我使用选项2安装了msysgit。(将git\bin目录添加到PATH环境变量中,但不覆盖一些内置的windows工具)

notepad++包装器被称为.bat这一事实并不重要。 最好将其命名为'npp.sh',并将其放在[git]\cmd目录中(或在由PATH环境变量引用的任何目录中)。


参见:

我如何查看' git diff '输出与视觉diff程序?对于一般理论 如何使用msysgit / gitk设置DiffMerge ?另一个外部工具的例子(DiffMerge和WinMerge)


Lightfire228在评论中补充道:

对于任何遇到n++只打开一个空白文件,git不接受你的提交消息的问题,请参阅“由于空消息而中止提交”:更改你的.bat或.sh文件如下:

"<path-to-n++" .git/COMMIT_EDITMSG -<arguments>. 

这将告诉notepad++打开临时提交文件,而不是一个空白的新文件。

我需要做以下两件事来让Git在Windows中启动notepad++:

将以下内容添加到.gitconfig中: editor = 'C:/Program Files/ notepad++ /notepad++.exe' -multiInst -notabbar -nosession -noPlugin .exe 修改快捷方式以管理员身份启动Git Bash shell,然后使用该快捷方式启动Git Bash shell。我猜上下文菜单项“Git Bash here”没有启动notepad++所需的权限。

在做了以上两件事之后,它起作用了。

我很难让Git与写字板、科莫多编辑器以及我给它的几乎所有其他编辑器合作。大多数打开以供编辑,但Git显然不会等待保存/关闭发生。

作为拐杖,我一直在做。

git commit -m "Fixed the LoadAll method"

让事情继续发展。它往往会使我的提交消息比应该的要短一些,但显然在Windows版本的Git上还有一些工作要做。

GitGUI也不是那么糟糕。它需要一点定位,但在那之后,它工作得很好。