git-delta

git-delta 是帶有語法高亮的比對工具。

你知道的,git 預設是使用 diff,diff 沒有語法高亮,而且在比對的顯示需要一點時間習慣。

git-delta :delta

安裝

安裝蠻簡單的,提供各種作業系統的安裝方法:Installation – delta

以 Debian/Ubuntu 來說,從下載網頁下載 debian package以後,用 dpkg -i 安裝就可以。

wget https://github.com/dandavison/delta/releases/download/0.15.1/git-delta_0.15.1_amd64.deb
sudo dpkg -i git-delta_0.15.1_amd64.deb

設定

編輯 ~/.gitconfig ,加上以下設定

[core]
    pager = delta

[interactive]
    diffFilter = delta --color-only --features=interactive

[delta]
    features = decorations

[delta "interactive"]
    keep-plus-minus-markers = false

[delta "decorations"]
    commit-decoration-style = blue ol
    commit-style = raw
    file-style = omit
    hunk-header-decoration-style = blue box
    hunk-header-file-style = red
    hunk-header-line-number-style = "#067a00"
    hunk-header-style = file line-number syntax

使用

設定加好以後,在使用以下 git 指令時,就會看到比對了

  • git diff
  • git show
  • git log -p
  • git stash show -p
  • git reflog -p
  • git add -p

也可以直接拿來替代 diff 使用

delta a.txt b.txt

其他還有很多設定可以調整,可以依照自己的需求來調整:Usage – delta

結語

這種可以提升開發速度的工具,要列到建立開發環境的步驟裡,這樣在開發上可以提升效率。

difftastic

終端機模式下檔案的比對是個很常見的場景,我知道的作法有兩個。

第一個是使用 diff -Naur <file1> <file2> ,這方法的好處是隨處可得,一般來說安裝完基本系統,就會有這個指令可以使用。缺點是不容易閱讀,你需要稍微知道他的規則才可以。

第二個方法是使用 vimdiff ,這個指令一般在安裝 vim 以後,就會有,需要熟悉 vim 操作,也需要知道 folding 的熱鍵怎麼用。好處是分左右兩邊呈現,容易閱讀。

後來知道有不錯的指令列工具可以使用,像是今天要介紹的 difftastic,他可以分左右兩邊呈現,可以單獨呼叫,也可以結合 git 使用,缺點就是要另外安裝,例如 RHEL/Ubuntu 還沒包這個套件。

安裝

下載位址:https://github.com/Wilfred/difftastic/tags

下載,解壓縮以後,把 difft 這個檔案複製到 /usr/local/bin 即可。

使用

difft <file1> <file2>

輸出結果

$ difft /tmp/foo.txt /tmp/bar.txt
/tmp/bar.txt --- Text
1 1111                                                       1 1214
2 22                                                         2 22
3 33                                                         3 99
4 44                                                         4 44
5 55                                                         5 55
6 66                                                         6 66

相異的地方有顏色呈現,很清楚,這部份可以參考 github 上的網頁

要搭配 git 使用,可以參閱手冊:https://difftastic.wilfred.me.uk/git.html

設定方法可以使用環境變數,也可以用 git config 來設定。

環境變數的方式

export GIT_EXTERNAL_DIFF=difft

使用 git config 的方式。

# Set git configuration for the current repository.
$ git config diff.external difft

# Set git configuration for all repositories.
$ git config --global diff.external difft

有了 difftastic,在比對檔案上就方便許多,省下許多看錯的困擾。