How use editor with “git commit”?

2019-09-06 03:29发布

问题:

I have installed fresh Ubuntu 12.04 and initialized some project with git. When I did git commit, it opened some file with nano editor for me to enter commit description.

Questions:

1) Can I use vi instead of nano and how?

2) Should I append proposed content or replace it?

回答1:

Others have indicated how to change the editor, but here are a couple more tips.

Firstly, a blank commit message aborts the commit. This is handy if you realise you have forgotten something while typing your message.

Secondly, there is a sort of informal standard for the creation of commit messages which it is a good idea to adhere to. By following the standard you ensure that logs, patches etc. work well. The standard is this

The first line should be a brief summary no more than 72 chars long (some say 50).

Then there should be a blank line, followed by a longer explanation which
can go on to as many lines as you like and use * or - etc. for bullet
points.

- Lines should be hard-wrapped with a carriage return.
- They should not be longer than 72 characters.

These are guidelines only, git does not enforce them and there is some variation in what projects expect, but they are good guidelines to stick to.



回答2:

1) yes, install vi(m) and use sudo update-alternatives –config editor
2) do as you like, lines starting with an # will be ignored



回答3:

Put export EDITOR=vi in your .profile file to set your default editor.

Commit messages should, generally, be short, so usually it's better to replace all that stuff with a short description. It's really there just so you can see what you're committing.



回答4:

You have to change your default editor. This can be done from the command line using the following command:

export EDITOR=vim

replacing vim with whatever the name of the editor you'd like to use is.

EDIT: I should also note that its common to use git commit -m "commit message here" instead of git commit, since commit messages generally aren't very long and don't necessitate pulling up an entire editor to write a quick sentence.



回答5:

For question 1:

You can try this: $ git config --global core.editor vi since vi is pre-installed on fresh Ubuntu 12.04.

From official manual of git config:

core.editor
    Commands such as commit and tag that lets you edit messages by 
    launching an editor uses the value of this variable when it is set,
    and the environment variable GIT_EDITOR is not set. See git-var(1).

For question 2:

From official manual of git commit:

--cleanup=<mode>
    This option sets how the commit message is cleaned up.
    The <mode> can be one of verbatim, whitespace, strip, and default.
    The default mode will strip leading and trailing empty lines and
    #commentary from the commit message only if the message is to be
    edited. Otherwise only whitespace removed. The verbatim mode does
    not change message at all, whitespace removes just leading/trailing
    whitespace lines and strip removes both whitespace and commentary.

As can see here, the default mode will strip leading and trailing empty lines and #commentary from the commit message only if the message is to be edited.

Other config options that may help:

commit.status
    A boolean to enable/disable inclusion of status information in the
    commit message template when using an editor to prepare the commit
    message. Defaults to true.

and

commit.template
    Specify a file to use as the template for new commit messages.
    "~/" is expanded to the value of $HOME and "~user/" to the specified
    user’s home directory.


标签: git commit vi nano