命令行使用git的正確姿勢--tig

截圖

在命令行下使用git的正確姿勢應該是在vim里面是用git插件,不過并不是所有人都喜歡Vim編輯器,這個時候你可以試下tig。這也是本文要講的東西,安裝方法很簡單、

請看Installation instruction,一般來說用你系統的包管理器安裝tig即可。

tig的配置文件為.tigrc,如何配置,將是本文的主題,主要是翻譯源碼中的幫助文檔,希望對你有用。

概述

你可以永久的設置選項通過把它放到~/.tigrc中。

  • tigrc中的每一行只能包含一個命令,
  • 你可以用\用來分隔長行。
  • #開始的語句被視為注釋。

除了在.tigrc中設置,你也可以在.gitconfig中新建一個標簽[tig]來做一樣的設置

示例:

[tig] show-changes = true
[tig "color"] cursor = yellow red bold 
[tig "bind"] generic = P parent

tigrc語法總結:

set variable = value
bind keymap key action
color area fgcolor bgcolor [attributes]
source path

變量

語法:

set variables = value

示例:

set commit-order = topo     # Order commits topologically
set git-colors = no     # Do not read Git's color settings.
set horizontal-scroll = 33% # Scroll 33% of the view width
set blame-options = -C -C -C    # Blame lines from other files

# Wrap branch names with () and tags with <>
set reference-format = (branch) <tag>

# Configure blame view columns using command spanning multiple lines.
set blame-view = \
    date:default \
    author:abbreviated \
    file-name:auto \
    id:yes,color \
    line-number:yes,interval=5 text

變量只有三種類型,要么是bool,要么是string要么是int,要么是混合的。

  • bool類型的變量,可能的取值是"true","yes"或者"1"
  • int類型的取值只能是非負整數;
  • string類型用單引號或者雙引號包括起來。
  • 混合的類型的取值會在下面的提到(只是包含有限的取值)

下面的變量可以設置:

  • diff-options (string)::
    取值是用空格隔開的字符串用于diff-view中。

  • blame-options (string)::
    取值是用空格隔開的字符串。可用于告訴git-blame如何檢測行的源頭。

  • log-options (string)::
    取值是用空格隔開的字符串用于Log-view中,用于告訴git-log如何顯示,實際上就是git-log的選項。

  • main-options (string)::
    取值是用空格隔開的字符串用于main-view中。作用同上。

  • reference-format (string)::
    用空格隔開的格式化字符串,用于格式化參考名字。

    Supported reference types are:

    • head : The current HEAD.
    • tag : A signed tag.
    • local-tag : An unsigned tag.
    • remote : A remote.
    • tracked-remote : The remote tracked by current HEAD.
    • replace : A replaced reference.
    • branch : Any other reference.
  • line-graphics (mixed) [ascii|default|utf-8|<bool>]::
    決定哪種類型的字符符號用于畫行。

  • horizontal-scroll (mixed)::
    水平滾動百分比(相對當前的屏幕),取值可以像"33",也可以是"33%"

  • git-colors (list)::
    取值由由空格隔開的"key=value"這樣的形式組成,其中的key是git的color
    名字,value是tig的color名字,比如"branch.current=main-head"和"grep.filename=grep.file"
    設置"no"禁止。

  • show-notes (mixed) [<reference>|<bool>]::
    是否選擇顯示commit的note,所設置的內容將傳遞給git show --notes=命令。Note默認是使能的。

  • show-changes (bool)::
    是否在main-view顯示stage或者unstage的狀態。

  • vertical-split (mixed) [auto|<bool>]::
    是垂直還是水平分隔view。auto表示將會根據屏幕大小來自動選擇方式。true的話表示垂直分隔。

  • split-view-height (mixed)::
    水平分割時的bottom view的高度(百分比)。

  • split-view-width (mixed)::
    垂直分割時的right-most view的寬度(百分比)。

  • status-untracked-dirs (bool)::
    是否顯示未跟蹤的目錄里的內容。默認是on。

  • tab-size (int)::
    每個tab占據space數量,默認是8。

  • diff-context (int)::
    diff view的時候diff上下文顯示行數。

  • diff-highlight (mixed)::
    是否使用Git的'diff-highlight'來高亮diffs。默認是false。

  • ignore-space (mixed) [no|all|some|at-eol|<bool>]::
    在diff-view中是否忽略空格改變。默認是不被忽略的。設置為all,, "some" or "at-eol" 相當于傳遞"--ignore-all-space", "--ignore-space" or
    "--ignore-space-at-eol"給git diff或者git show

  • commit-order (enum) [auto|default|topo|date|author-date|reverse]::
    選擇commit順序。可選有:chronological reverse, topological order,
    date orderreverse order。用于當commit
    graphics在main-view中使能。在log很長的倉庫中推薦設置這個選項為default,這樣有助于加快加載的速度。

  • ignore-case (bool)::
    搜索的時候是否大小敏感。默認是大小敏感。

  • mailmap (bool)::
    從.mailmap讀取名字和email,默認是off。請看git-shortlog(1)

  • wrap-lines (bool)::
    是否warp長行,默認是不進行warp。

  • focus-child (bool)::
    當子視圖打開時是否聚焦在子視圖上,默認是true。

  • editor-line-number (bool)::
    是否傳遞行號給編輯器。行號以+<line-number>加到命令行選項中文件名的前面,比如說:vim +10 tig.c

  • mouse (bool)::
    是否支持鼠標。默認off。

  • mouse-scroll (int)::
    鼠標滾動距離,默認是3行。必須設置mouse為true才能讓這個選項有效。

  • refresh-mode (mixed) [manual|auto|after-command|periodic|<bool>]::
    控制視圖根據倉庫里面的文件的修改情況何刷新.當設置為"auto"的時候,當檢測到文件修改的時候視圖會自動刷新。當設置為manual的時候,不會自動刷新。當設置為after-command,只有在外部命令執行完畢之后才會刷新。當設置為periodic,就是周期刷新,周期根據變量refresh-interval

  • refresh-interval (int)::
    周期刷新的間隔。

  • file-args (args)::
    命令行參數傳遞給git-rev-parse(1)

  • rev-args (args)::
    命令行參數傳遞給git-rev-parse(1)

視圖(view)變量設置

視圖設置定義了視圖中不同列的順序和選項。每個視圖設置期待一個用空格隔開的列標準。

列標準以列類型開始,后面可選的加一個冒號接著列選項列表。比如說author:email,width=20

列標準的第一個選項總是display相關的選項,當沒有給display的值,那么默認給yes
對于那些期待枚舉值的display選項將自動解決默認的枚舉值,比如說file-name它的
display選項將自動設置為auto

標準允許單列。比如覆蓋系統tigrc的配置。為了覆蓋單個列,使用列名作為視圖名的后綴。比如
說:main-view-date

示例:

# Enable both ID and line numbers in the blame view
set blame-view = date:default author:full file-name:auto id:yes,color \
         line-number:yes,interval=5 text

# Change grep view to be similar to `git grep` format
set grep-view = file-name:yes line-number:yes,interval=1 text

# Show file sizes as units
set tree-view = line-number:no,interval=5 mode author:full \
        file-size:units date:default id:no file-name

# Show line numbers for every 10th line in the pager view
set pager-view = line-number:yes,interval=10 text

# Shorthands to change view settings for a previously defined column
set main-view-date = custom
set main-view-date-format = "%Y-%m-%d %H:%M"
set blame-view-line-number = no
# Use Git's default commit order, even when the commit graph is enabled.
set commit-order = default

下面這些列類型是這些視圖所支持的。

blob-view, diff-view, log-view, pager-view, stage-view:: line-number, text
blame-view:: author, date, file-name, id, line-number, text
grep-view:: file-name, line-number, text
main-view:: author, date, commit-title, id, line-number
refs-view:: author, date, commit-title, id, line-number, ref
stash-view:: author, date, commit-title, id, line-number
status-view:: file-name, line-number, status
tree-view:: author, date, id, file-name, file-size, line-number, mode

列類型以及它們的列選項:

  • author:
    1. display(mixed) [full|abbreviated|email|email-user|<bool>]: 如何顯示author的名字
      如果設置為abbreviated,那么author將以縮寫來顯示。
    2. width (int):列的寬度。如果范圍是1到10,那么author名將以縮寫來顯示,當設置為0,那么將根據
      名字長度來顯示。
  • commit-title:
    1. graph(mixed) [no|v2|v1]:是否在main-view顯示版本圖形。v1表示舊的圖形渲染,它雖然不太準確但是
      更快。請看line-graph選項。
    2. refs(bool):在main-view是否顯示引用(分支,tags和遠程)
    3. overflow(bool or int):當commit文本超過給定的寬度是否在commit標題高亮超出的文本。當為bool值的時候那么默認的寬度是50,當為int值則以這個值為寬度。
  • date:
    1. display (mixed) [relative|relative-compact|custom|default|<bool>]:如何顯示日期。如果設置為relative或者relative-compact,那么將顯示一個相對的日期,比如“2分鐘之前”。如果設置為"custom",那么strftime(3)字符串格式將在format選項中指定。
    2. local(bool):如果是true,那么將使用localtime(3)來轉換時區。注意相對時間將總是使用本地便宜。
    3. format(string):當display的模式選擇為custom的時候,傳給strftime(3)的格式化字符串。
    4. width(int):列的寬度,設置為0將自動適應。
  • file-name:
    1. display(mixed) [auto|always|<bool>]:什么時候顯示文件名。如果設置為auto,那么文件名只有需要才會顯示。
    2. width(int):列的寬度。同上。
  • file-size::
    1. display (mixed) [default|units|<bool>]:如何顯示文件的大小。當設置為units的時候,sizes將以二進制后綴來顯示,比如說12524將顯示為"12.2K"。
    2. width(int):同上
  • id::
    1. display (bool):是否在main-view中顯示commit ID
    2. width(int):commit ID的寬度,如果沒有設置,tig將會使用core.abbrev的git設置。當設置為0將自動適應reflog。
  • line-number::
    1. display (bool):是否顯示行號。
    2. interval (int):行號之間的間隔
    3. width (int):列的寬度
  • mode::
    1. display (bool):是否顯示文件模式。
    2. width (int): 同上。
  • ref::
    1. display (bool):是否顯示引用名(分支名,標簽等)
    2. 'width' (int):同上。
  • status::
    1. 'display' (mixed) [no|short|long|<bool>]:如何顯示狀態標號。
    2. 'width' (int):同上。
  • text::
    1. 'commit-title-overflow' (bool or int):是否高亮那些超出指定字符長度的commit標題。默認長度是50,當為int值時,就是這個值作為最大長度。

所有的列選項都可以被toggle。形式::toggle coloumname-option

Bind命令

用于映射快捷鍵到指定動作。

語法:

bind keymap key action

示例:

# Add keybinding to quickly jump to the next diff chunk in the stage view
bind stage <Enter> :/^@@

# Disable the default mapping for running git-gc
bind generic G none

# User-defined external command to amend the last commit
bind status + !git commit --amend

# User-defined internal command that reloads ~/.tigrc
bind generic S :source ~/.tigrc

# UTF8-encoded characters can be used as key values.
bind generic ? @sh -c "printf '%s' %(commit) | pbcopy"

也可以在gitconfig中設置:

[tig "bind"]
    # 'unbind' the default quit key binding
    main = Q none
    # Cherry-pick current commit onto current branch
    generic = C !git cherry-pick %(commit)

keymap指定按鍵映射的范圍,首先是在當前的view中搜索,找不到在從generic中搜。

view按鍵映射將會覆蓋generic的,而它們都會覆蓋內置的按鍵映射的。

Keymaps::

有效的范圍是: main, diff, log, help, pager, status, stage,
tree, blob, blame, refs, stash, grep and generic. 使用 generic
作為所有視圖的按鍵映射. 使用 search 作為在搜索時的按鍵操作。

Key values::

按鍵值不能被引號括起來。可以使用ASCII或者UTF8編碼的字符或者下面這些符號名。符號按鍵名是大小不敏感的,
<>括起來。使用<Hash>來綁定#按鍵,用<LessThan>作為<

一些Keycode:

*<Enter>*, *<Space>*, *<Backspace>*, *<Tab>*, *<Escape>* or *<Esc>*, *<Left>*,
*<Right>*, *<Up>*, *<Down>*, *<Insert>* or *<Ins>*, *<Delete>* or *<Del>*,
*<Hash>*, *<LessThan>* or *<LT>*, *<Home>*, *<End>*, *<PageUp>* or *<PgUp>*,
*<PageDown>* or *<PgDown>*, *<F1>*, *<F2>*, *<F3>*, *<F4>*, *<F5>*, *<F6>*,
*<F7>*, *<F8>*, *<F9>*, *<F10>*, *<F11>*, *<F12>*.

映射以Ctrl開始的按鍵,使用<Ctrl-key>.

例子:

bind main R     refresh
bind main <Down>    next
bind main <Ctrl-f>  scroll-page-down
bind main <Esc>o    options

由于ncurse的ctrl編碼,其中Ctrl-mCtrl-i 不能被綁定,因為他們與EnterTab沖突。還有,
ncurse無法區分Ctrl-fCtrl-F兩個按鍵映射,另外Ctrl-z也是作為后臺執行的按鍵不能使用。

Actions::

既可以是用戶定義的命令(外部或者內部的)或者使用下面章節提到的action名字。

外部用戶自定義命令

外部命令以一個或者多個選項標志開始:

!: 在前臺運行外部命令并輸出。
@: 在后臺運行命令并且禁止輸出
?: 在運行命令前彈出確認
<: 運行命令后退出

例子:?<git commit將會彈出確認提示,然后執行命令行退出tig

訪問狀態變量

用戶定義的命令可以訪問Tig的內部狀態變量。在執行命令之前它們將被具體內容展開再傳入命令中。

變量 說明
%(head) 當前view的head ID,默認是HEAD
%(commit) 當前被選中的commit id
%(blob) 當前被選中的blob ID.
%(branch) 當前被選中的分支名
%(remote) 當前被選中的遠程名字.
%(tag) 當前被選中的tag名字
%(stash) 當前被選中的stash名字
%(directory) 在tree視圖中的路徑名,或者"."如果沒定義的話。
%(file) 當前選中文件名
%(lineno) 當前選擇的文件名
%(ref) 當前blame的引用,或者HEAD如果沒有定義的話。
%(revargs) 傳遞給命令行的版本號
%(fileargs) 傳遞給命令行文件參數
%(cmdlineargs) 其它所有的傳遞給命令行的參數
%(diffargs) 用于diff和stage視圖來自diff-options或者TIG_DIFF_OPTS
%(blameargs) 用于blame視圖的來自于選項blame-options的選項。
%(logargs) 用于log視圖的來自于選項log-options的選項
%(mainargs) 用于main視圖的來自于選項main-options的選項
%(prompt) 彈出提示輸入參數的值。也可以用 "%(prompt Enter branch name: )"
%(text) 當前行的文字
%(repo:head) 當前checkout的分支名
%(repo:head-id) 當前checkout的分支的id
%(repo:remote) 當前:遠程名/分支名
%(repo:cdup) 將要轉到的相對于repo根目錄的相對路徑名,比如../
%(repo:prefix) 當前工作目錄的前綴 e.g subdir/.
%(repo:git-dir) repo的.git所在決定路徑, e.g. /src/repo/.git.
%(repo:is-inside-work-tree) tig是否運行在work tree中,true或者false
# Save save the current commit as a patch file when the user selects a
# commit in the main view and presses 'S'.
bind main S !git format-patch -1 %(commit)

# Create and checkout a new branch; specify custom prompt
bind main B ?git checkout -b "%(prompt Enter new branch name: )"

高級的類似shell的命令

如果你對命令需要用到一些動態的特性,比如說子shell,可以這樣

bind generic I @sh -c "echo -n %(commit) | xclip -selection c"

或者結合git config的alias和Tig的外部命令:

[alias]
    gitk-bg = !"gitk HEAD --not $(git rev-parse --remotes) &"
    publish = !"for i in origin public; do git push $i; done"
[tig "bind"]
    # @-prefix means that the console output will not be shown.
    generic = V !@git gitk-bg
    generic = > !git publish

內部用戶自定義命令

:開始的的命令,將被視為Tig的內部命令,可用的內部命令就是那些配置文件選項(就像本文提到的)還有paper view的命令,例子:

# Reload ~/.tigrc when 'S' is pressed
bind generic S :source .tigrc

# Change diff view to show all commit changes regardless of file limitations
bind diff F :set diff-options = --full-diff

# Show the output of git-reflog(1) in the pager view
bind generic W :!git reflog

# Search for previous diff (c)hunk and next diff header
bind stage 2 :?^@@
bind stage D :/^diff --(git|cc)

bind main I :toggle id              # Show/hide the ID column
bind diff D :toggle diff-options --minimal  # Use minimal diff algorithm
bind diff [ :toggle diff-context -3     # Decrease context (-U arg)
bind diff ] :toggle diff-context +3     # Increase context
bind generic V :toggle split-view-height -10%   # Decrease split height

內部Action符號

所有符號都是大小不敏感的,并且"view-main", "View.Main", and "VIEW_MAIN"
這三種形式的一樣的。

視圖切換:

符號 說明
view-main 顯示main視圖
view-diff 顯示diff視圖
view-log 顯示log視圖
view-tree 顯示tree視圖
view-blob 顯示blob視圖
view-blame 顯示blame視圖
view-refs 顯示refs視圖
view-status 顯示status視圖
view-stage 顯示stage視圖
view-stash 顯示stash視圖
view-grep 顯示grep視圖
view-pager 顯示pager視圖
view-help 顯示help視圖

視圖操作:

符號 說明
enter 進入并且打開選擇的行
back 返回到上一個視圖狀態
next 移動到下一個視圖
previous 移動到前一個視圖
parent 移動到父視圖
view-next 移動當前聚焦到下一個視圖
refresh 重載和刷新視圖
maximize 最大化當前視圖
view-close 關閉當前視圖
quit 關閉所有視圖并退出

視圖特殊操作:

符號 說明
status-update Stage/unstage 文件s
status-revert Revert 文件
status-merge 使用外部文件來合并
stage-update-line Stage/unstage 單一文件
stage-split-chunk 分隔當前diff chunk

光標導航:

符號 說明
move-up 移動光標 向上一行
move-down 移動光標 向下一行
move-page-down 移動光標 向下翻頁
move-page-up 移動光標 向上翻頁
move-half-page-down 移動光標 向下半頁
move-half-page-up 移動光標 想上半頁
move-first-line 移動光標 到第一行
move-last-line 移動光標 到最后一行
move-next-merge 移動光標 下一個合并的commit
move-prev-merge 移動光標 上一個合并的commit

滾動相關:

符號 說明
scroll-line-up 向上滾動一行
scroll-line-down 向下滾動一行
scroll-page-up 向上滾動一頁
scroll-page-down 向下滾動一頁
scroll-first-col 移動到行首
scroll-left 向左移動兩列
scroll-right 向右移動兩列

搜索相關:

符號 說明
search 搜索
search-back 向后搜索
find-next 查找下一個匹配的
find-prev 查找上一個匹配的

其它:

符號 說明
edit 在編輯器中打開
prompt 打開提示
options 打開選項菜單
screen-redraw 重畫屏幕
stop-loading 停止所有加載的視圖
show-version 顯示版本信息
none 什么也做

顏色命令

如果你對終端支持顏色的話,這個顏色命令可用于高亮用戶界面的。

語法:

color area fgcolor bgcolor [attributes]

# Override the default terminal colors to white on black.
color default       white   black
# Diff colors
color diff-header   yellow  default
color diff-index    blue    default
color diff-chunk    magenta default
color "Reported-by:"    green   default
# View specific color
color tree.date     black   cyan    bold

Area名字

既可以是內置的area名字也可以自定義的被括起來的字符串。例子:
e.g. "stage.diff-chunk" and "diff.diff-chunk".

顏色名字

可用顏色包括: *white*, *black*, *green*, *magenta*, *blue*,
*cyan*, *yellow*, *red*, *default*. 使用 *default* 表示使用終端的顏色。

屬性名字

可用屬性包括:*normal*, *blink*, *bold*, *dim*, *reverse*,
*standout*, and *underline*。注意,不是所有屬性都被終端所支持。

UI顏色

UI 說明
default Override default terminal colors (see above).
cursor The cursor line.
status The status window showing info messages.
title-focus The title window for the current view.
title-blur The title window of any backgrounded view.
search-result Highlighted search result.
delimiter Delimiter shown for truncated lines.
header The view header lines. Use 'status.header' to color the staged, unstaged, and untracked sections in the status view. Use 'help.header' to color the keymap sections in the help view.
line-number Line numbers.
id The commit ID.
date The author date.
author The commit author.
mode The file mode holding the permissions and type.
overflow Title text overflow.
directory The directory name.
file The file name.
file-size File size.

main視圖

UI 說明
graph-commit The commit dot in the revision graph.
palette-[0-13] 14 different colors, used for distinguishing branches or commits. By default, the palette uses the ASCII colors, where the second half of them have the bold attribute enabled to give a brighter color.Example: palette-0 = red
main-commit The commit comment.
main-head Label of the current branch.
main-remote Label of a remote.
main-tracked Label of the remote tracked by the current branch.
main-tag Label of a signed tag.
main-local-tag Label of a local tag.
main-ref Label of any other reference.
main-replace Label of replaced reference.

status視圖

UI 說明
stat-none Empty status label.
stat-staged Status flag of staged files.
stat-unstaged Status flag of unstaged files.
stat-untracked Status flag of untracked files.

help視圖

UI 說明
help-group Help group name.
help-action Help action name.

高亮

Diff 增強

關于diff開始,塊和行增加刪除。

diff-header, diff-chunk, diff-add, diff-add2, diff-del,
diff-del2

通過Git diff機制發出的額外的diff信息,比如說模式改變,重命名的檢測等。

diff-oldmode, diff-newmode, diff-copy-from, diff-copy-to,
diff-similarity, diff-index

漂亮的commit頭部

pp-refs, pp-reflog, pp-reflogmsg, pp-merge

commit, parent, tree, author, committer

Signed-off-by, Acked-by, Reviewed-by and Tested-by lines are colorized.
Characters in the commit title exceeding a predefined width can be highlighted.

樹增強

tree-dir, tree-file

source命令

source命令讓讀取另外的配置文件成為可能。

語法:

source 'path'

示例:

source ~/.tig/colorscheme.tigrc
source ~/.tig/keybindings.tigrc

一些有用的個人經驗

bind按鍵的時候,有可能會有一些沖突的導致你的按鍵映射無效。

比如說你在status視圖中映射cc做一些事情,但是發現怎么弄都無法生效的,原因是Tig默認已經為cgeneric的范圍映射了某個功能。

這時候你需要的做的是

bind staus c none

這樣做之后在status視圖以c的前綴的按鍵映射就可以用了。

參考鏈接

  1. 官方一些模板配置

  2. 我自己tigrc

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容