在 OS X 上使用 vi 添加 git commit 消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33504984/
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-19 11:29:33  来源:igfitidea点击:

Adding a git commit message using vi on OS X

gitmacosvimmergevi

提问by GitForJava

I'm learning using Git on an OS X Terminal. It seems really easy. But I can't handle just one problem: When I try to merge two branches, for example "myTestBranch" into "master" this program covers the terminal and shows me a new view where I should write the merge message. And then, I don't know how to do "Enter", save the merge message and then come back to the main terminal view where I can continue working.

我正在学习在 OS X 终端上使用 Git。看起来真的很容易。但是我不能只处理一个问题:当我尝试将两个分支合并时,例如将“myTestBranch”合并到“master”中时,该程序会覆盖终端并向我显示一个新视图,我应该在其中编写合并消息。然后,我不知道如何“输入”,保存合并消息,然后返回到主终端视图,我可以在那里继续工作。

Does anyone know, how does it work?

有谁知道,它是如何工作的?

What I see, when i try to merge

我所看到的,当我尝试合并时

回答by Raúl Oma?a

If you haven't change the default git's editor, that "new view" is the Vi program.

如果你没有改变默认的 git 编辑器,那个“新视图”就是 Vi 程序。

To save your commit message using Vi, follow the next steps:

要使用 Vi 保存提交消息,请执行以下步骤:

  1. Type i
  2. Write your message
  3. Type the ESCkey
  4. Type :wq
  5. DONE! :D
  1. 类型 i
  2. 写下你的信息
  3. 键入ESC密钥
  4. 类型 :wq
  5. 完毕!:D

Typing :q, step 4, is not enough beacuse just means QUITwithout saving. That's why you need :wq, which means WRITEand QUIT.

键入:q,第 4 步是不够的,因为仅意味着QUIT不保存。这就是为什么你需要:wq,这意味着WRITEQUIT

You can write your commit message using your favourite editor(vim, emacs, etc.). To achieve this, you can use configuration parameter or environment variables, listed in order:

您可以使用自己喜欢的编辑器(vim、emacs 等)编写提交消息。为此,您可以使用按顺序列出的配置参数或环境变量:

  1. GIT_EDITORenvironment variable
  2. core.editorconfiguration option
  3. VISUALenvironment variable
  4. EDITORenvironment variable
  1. GIT_EDITOR环境变量
  2. core.editor配置选项
  3. 视觉环境变量
  4. 编辑器环境变量

Using the configuration option type something like this:

使用配置选项键入如下内容:

$git config --global core.editor "nano"

Or if you want to use enviroment variables, add something like this to your .bash_profile

或者,如果您想使用环境变量,请将这样的内容添加到您的 .bash_profile

$export GIT_EDITOR="PATH/TO/YOUR/EDITOR"

回答by Jelle

By default Git will open Vim as editor.

默认情况下,Git 会以编辑器的形式打开 Vim。

You basically need to type 'I' to start editing. After that ESCand type :qto quit or :wto save the file. You can also combine them: :wqto save and exit Vim.

您基本上需要输入“I”才能开始编辑。之后,ESC键入:q退出或:w保存文件。你也可以组合它们::wq保存和退出 Vim。

For more information about Vim check the official documentation

有关 Vim 的更多信息,请查看官方文档

To change Vim for any other editor check the Git Environment Variablesor older posts with a similar question: How do I make Git use the editor of my choice for commits?

要为任何其他编辑器更改 Vim,请查看Git 环境变量或带有类似问题的旧帖子: 如何让 Git 使用我选择的编辑器进行提交?