【Git Snippets】The rest of basic configuration

507 阅读1分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

The Git snippet collection contains a variety of short tips and tricks for all currently maintained versions of git. It includes most commonly-used commands and covers various use-cases in the form of simplified documentation, complete with multiple examples.

Short code snippets for all your development needs.

Add a commit message template

Sets up a commit message template for the current repository.

  • Use git config commit.template <file> to specify <file> as the commit message template for the current repository.
$ git config commit.template <file>

Example

# Sets message of template.txt as the commit message template
$ git config commit.template template.txt

template.txt:

config and then commit:

commit.template just provide a template-message to reduce the time it takes to edit your submissions, you need to edit the template-message anyhow.
Once you did not edit the message: Aborting commit !

Configure git user information

Configures user information for git.

  • Use git config user.email <email> to set the user's email for the current repository.
  • Use git config user.name <name> to set the user's name for the current repository.
  • You can use the --global flag to configure global user information.
$ git config [--global] user.email <email>
$ git config [--global] user.name  <name>

Example

# Configures user for current repository
$ git config user.email "cool.duck@qua.ck"
$ git config user.name "Duck Quackers"

# Configures global git user
$ git config --global user.email "cool.duck@qua.ck"
$ git config --global user.name "Duck Quackers"

Configure line endings

Configures the line endings for a repository.

  • Use git config core.eol [lf | crlf] to configure the line endings.
  • lf is the UNIX line endings (\n), whereas crlf is the DOS line ending (\r\n).
$ git config core.eol [lf | crlf]

Example

# Configured to use UNIX line endings
$ git config core.eol lf

For more information about this knowledge: About CRLF & LF

Autocorrect git commands

Configure git to autocorrect mistyped commands.

  • Use git config --global help.autocorrect 1 to enable git's autocorrect.
$ git config --global help.autocorrect 1

Example

$ git config --global help.autocorrect 1
# Runs `git status` instead
$ git sttaus

Configure the git text editor

Configure the text editor used by git.

  • Use git config --global core.editor <editor-command> to call <editor-command> as the git text editor.
$ git config --global core.editor <editor-command>

Example

# Sets 'VS Code' as the git text editor
$ git config --global core.editor "code --wait"
# Sets 'vim' as the git text editor
$ git config --global core.editor "vim"

🧠Wish you can benefit from this article.
🔥 And welcome to leave your thoughts in the comments section that we can discuss and share ideas with each other.