Git出力に色を割り当てるには?

Git出力に色を割り当てるには?

Git(または他のコマンド)の出力に色を付ける方法はありますか?

考慮する:

baller@Laptop:~/rails/spunky-monkey$ git status
# On branch new-message-types
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   app/models/message_type.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
baller@Laptop:~/rails/spunky-monkey$ git add app/models

そして

baller@Laptop:~/rails/spunky-monkey$ git status
# On branch new-message-types
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   app/models/message_type.rb
#

出力は同じように見えますが、情報はまったく異なります。つまり、ファイルが送信の準備ができていない状態から準備済みの状態に変更されました。

出力に色を割り当てる方法はありますか?たとえば、ステージングされていないファイルは赤色で、ステージングされたファイルは緑色です。

それともChanges not staged for commit:赤と# Changes to be committed:緑もありますか?

Ubuntuで働いています。

編集:Google検索で本当にうまく機能する答えが公開されましたgit config --global --add color.ui true

しかし、コマンド出力に色を追加するより一般的な解決策はありますか?

答え1

[color]~/.gitconfigたとえば、次を含むセクションを作成できます。

[color]
  diff = auto
  status = auto
  branch = auto
  interactive = auto
  ui = true
  pager = true

また、色付け方法を細かく制御できます。

[color "status"]
  added = green
  changed = red bold
  untracked = magenta bold

[color "branch"]
  remote = yellow

これがあなたが始めることを願っています。もちろん、色をサポートする端末が必要です。

また見てくださいこの回答コマンドラインから直接シェーディングを追加する方法を学びます。

答え2

使いたいかもしれません

git config --global color.ui auto

このautoセクションでは、gitはそれをサポートする端末でのみ色を使用しようとします。gitコマンドの出力をファイルにリダイレクトすると、ANSIシーケンスを取得できないと言います。 Git 1.8.4以降、デフォルトでもtrue同じに設定します。auto

これは、gitコマンドで使用できるcolor.uiさまざまな構成をすべて含むメタ構成です。color.*

これについてはで詳細に説明しますgit help config

color.ui設定すると、端末に出力しない限り色は印刷されないため、配管always出力時にも常にANSIカラー文字をエクスポートします。git log | lessauto

答え3

許容される回答は最も一般的な解決策を提供します。何らかの理由で設定を永久に変更する必要がない場合(このソリューションでは変更可能)、単一のgitコマンドの設定を上書きできます。

git -c color.ui=always <usual git command and options>

たとえば、

git -c color.ui=always status
git -c color.ui=always diff

テスト:git 2.4.6でサポートされている、いいえGit 1.7.1でサポートされています。

答え4

git config --global color.ui always
git config --global color.branch always
git config --global color.diff always
git config --global color.interactive always
git config --global color.status always
git config --global color.grep always
git config --global color.pager true
git config --global color.decorate always
git config --global color.showbranch always

関連情報