Git config 命令
更新时间: 2019-07-13 17:17
获取和设置配置变量。
语法
git config [<file-option>] [type] [--show-origin] [-z|--null] name [value [value_regex]]
git config [<file-option>] [type] --add name value
git config [<file-option>] [type] --replace-all name value [value_regex]
git config [<file-option>] [type] [--show-origin] [-z|--null] --get name [value_regex]
git config [<file-option>] [type] [--show-origin] [-z|--null] --get-all name [value_regex]
git config [<file-option>] [type] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
git config [<file-option>] [type] [-z|--null] --get-urlmatch name URL
git config [<file-option>] --unset name [value_regex]
git config [<file-option>] --unset-all name [value_regex]
git config [<file-option>] --rename-section old_name new_name
git config [<file-option>] --remove-section name
git config [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
git config [<file-option>] --get-color name [default]
git config [<file-option>] --get-colorbool name [stdout-is-tty]
git config [<file-option>] -e | --edit
参数
--replace-all
Default behavior is to replace at most one line. This replaces all lines matching the key (and optionally the value_regex).
--add
Adds a new line to the option without altering any existing values. This is the same as providing ^$ as the value_regex in
--replace-all.
--get
Get the value for a given key (optionally filtered by a regex matching the value). Returns error code 1 if the key was not found
and the last value if multiple key values were found.
--get-all
Like get, but returns all values for a multi-valued key.
--get-regexp
Like --get-all, but interprets the name as a regular expression and writes out the key names. Regular expression matching is
currently case-sensitive and done against a canonicalized version of the key in which section and variable names are lowercased,
but subsection names are not.
--get-urlmatch name URL
When given a two-part name section.key, the value for section.<url>.key whose <url> part matches the best to the given URL is
returned (if no such key exists, the value for section.key is used as a fallback). When given just the section as name, do so for
all the keys in the section and list them. Returns error code 1 if no value is found.
--global
For writing options: write to global ~/.gitconfig file rather than the repository .git/config, write to $XDG_CONFIG_HOME/git/config
file if this file exists and the ~/.gitconfig file doesn't.
For reading options: read only from global ~/.gitconfig and from $XDG_CONFIG_HOME/git/config rather than from all available files.
See also the section called "FILES".
--system
For writing options: write to system-wide $(prefix)/etc/gitconfig rather than the repository .git/config.
For reading options: read only from system-wide $(prefix)/etc/gitconfig rather than from all available files.
See also the section called "FILES".
--local
For writing options: write to the repository .git/config file. This is the default behavior.
For reading options: read only from the repository .git/config rather than from all available files.
See also the section called "FILES".
-f config-file, --file config-file
Use the given config file instead of the one specified by GIT_CONFIG.
--blob blob
Similar to --file but use the given blob instead of a file. E.g. you can use master:.gitmodules to read values from the file
.gitmodules in the master branch. See "SPECIFYING REVISIONS" section in gitrevisions(7) for a more complete list of ways to spell
blob names.
--remove-section
Remove the given section from the configuration file.
--rename-section
Rename the given section to a new name.
--unset
Remove the line matching the key from config file.
--unset-all
Remove all lines matching the key from config file.
-l, --list
List all variables set in config file, along with their values.
--bool
git config will ensure that the output is "true" or "false"
--int
git config will ensure that the output is a simple decimal number. An optional value suffix of k, m, or g in the config file will
cause the value to be multiplied by 1024, 1048576, or 1073741824 prior to output.
--bool-or-int
git config will ensure that the output matches the format of either --bool or --int, as described above.
--path
git config will expand a leading ~ to the value of $HOME, and ~user to the home directory for the specified user. This option has
no effect when setting the value (but you can use git config section.variable ~/ from the command line to let your shell do the
expansion).
-z, --null
For all options that output values and/or keys, always end values with the null character (instead of a newline). Use newline
instead as a delimiter between key and value. This allows for secure parsing of the output without getting confused e.g. by values
that contain line breaks.
--name-only
Output only the names of config variables for --list or --get-regexp.
--show-origin
Augment the output of all queried config options with the origin type (file, standard input, blob, command line) and the actual
origin (config file path, ref, or blob id if applicable).
--get-colorbool name [stdout-is-tty]
Find the color setting for name (e.g. color.diff) and output "true" or "false". stdout-is-tty should be either "true" or "false",
and is taken into account when configuration says "auto". If stdout-is-tty is missing, then checks the standard output of the
command itself, and exits with status 0 if color is to be used, or exits with status 1 otherwise. When the color setting for name
is undefined, the command uses color.ui as fallback.
--get-color name [default]
Find the color configured for name (e.g. color.diff.new) and output it as the ANSI color escape sequence to the standard output.
The optional default parameter is used instead, if there is no color configured for name.
-e, --edit
Opens an editor to modify the specified config file; either --system, --global, or repository (default).
--[no-]includes
Respect include.* directives in config files when looking up values. Defaults to off when a specific file is given (e.g., using
--file, --global, etc) and on when searching all config files.
使用示例
# 显示当前的Git配置
$ git config --list
# 编辑Git配置文件
$ git config -e [--global]
# 设置提交代码时的用户信息
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"
查看更多 git config
命令的使用方法,可以使用命令:
git help config