[git] How to color the Git console?

With Git 2.18, you have more control on how you want to specify colors in the console.
The "git config" command uses separate options e.g. "--int", "--bool", etc. to specify what type the caller wants the value to be interpreted as.

A new "--type=<typename>" option has been introduced, which would make it cleaner to define new types.

See commit fb0dc3b (18 Apr 2018), and commit 0a8950b (09 Apr 2018) by Taylor Blau (ttaylorr).
(Merged by Junio C Hamano -- gitster -- in commit e3e042b, 08 May 2018)

builtin/config.c: support --type=<type> as preferred alias for --<type>

git config has long allowed the ability for callers to provide a 'type specifier', which instructs git config to (1) ensure that incoming values can be interpreted as that type, and (2) that outgoing values are canonicalized under that type.

In another series, we propose to extend this functionality with --type=color and --default to replace --get-color.

However, we traditionally use --color to mean "colorize this output", instead of "this value should be treated as a color".

Currently, git config does not support this kind of colorization, but we should be careful to avoid squatting on this option too soon, so that git config can support --color (in the traditional sense) in the future, if that is desired.

In this patch, we support --type=<int|bool|bool-or-int|...> in addition to --int, --bool, and etc.
This allows the aforementioned upcoming patch to support querying a color value with a default via --type=color --default=..., without squandering --color.

We retain the historic behavior of complaining when multiple, legacy-style --<type> flags are given, as well as extend this to conflicting new-style --type=<type> flags. --int --type=int (and its commutative pair) does not complain, but --bool --type=int (and its commutative pair) does.

So before you had --bool and --int, now (documentation):

--type <type>

'git config' will ensure that any input or output is valid under the given type constraint(s), and will canonicalize outgoing values in <type>'s canonical form.

Valid <type>'s include:

  • 'bool': canonicalize values as either "true" or "false".
  • 'int': canonicalize values as simple decimal numbers. An optional suffix of 'k', 'm', or 'g' will cause the value to be multiplied by 1024, 1048576, or 1073741824 upon input.
  • 'bool-or-int': canonicalize according to either 'bool' or 'int', as described above.
  • 'path': canonicalize by adding a leading ~ to the value of $HOME and ~user to the home directory for the specified user. This specifier 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.)
  • 'expiry-date': canonicalize by converting from a fixed or relative date-string to a timestamp. This specifier has no effect when setting the value.
--bool::
--int::
--bool-or-int::
--path::
--expiry-date::
  Historical options for selecting a type specifier. Prefer instead `--type`,
(see: above).

Note that Git 2.22 (Q2 2019) explains "git config --type=color ..." is meant to replace "git config --get-color", but there is a slight difference that wasn't documented, which is now fixed.

See commit cd8e759 (05 Mar 2019) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit f6c75e3, 20 Mar 2019)

config: document --type=color output is a complete line

Even though the newer "--type=color" option to "git config" is meant to be upward compatible with the traditional "--get-color" option, unlike the latter, its output is not an incomplete line that lack the LF at the end.
That makes it consistent with output of other types like "git config --type=bool".

Document it, as it sometimes surprises unsuspecting users.

This now reads:

--type=color [--default=<default>] is preferred over --get-color (but note that --get-color will omit the trailing newline printed by --type=color).


You can see git config --type=bool used with Git 2.26 (Q1 2020) to replace "git config --bool" calls in sample templates.

See commit 81e3db4 (19 Jan 2020) by Lucius Hu (lebensterben).
(Merged by Junio C Hamano -- gitster -- in commit 7050624, 30 Jan 2020)

templates: fix deprecated type option --bool

Signed-off-by: Lucius Hu

The --bool option to git-config is marked as historical, and users are recommended to use --type=bool instead.
This commit replaces all occurrences of --bool in the templates.

Also note that, no other deprecated type options are found, including --int, --bool-or-int, --path, or --expiry-date.

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to colors

is it possible to add colors to python output? How do I use hexadecimal color strings in Flutter? How do I change the font color in an html table? How do I print colored output with Python 3? Change bar plot colour in geom_bar with ggplot2 in r How can I color a UIImage in Swift? How to change text color and console color in code::blocks? Android lollipop change navigation bar color How to change status bar color to match app in Lollipop? [Android] How to change color of the back arrow in the new material theme?

Examples related to console

Error in MySQL when setting default value for DATE or DATETIME Where can I read the Console output in Visual Studio 2015 Chrome - ERR_CACHE_MISS Swift: print() vs println() vs NSLog() Datatables: Cannot read property 'mData' of undefined How do I write to the console from a Laravel Controller? Cannot read property 'push' of undefined when combining arrays Very simple log4j2 XML configuration file using Console and File appender Console.log not working at all Chrome: console.log, console.debug are not working