git diff コマンド
参考:
git-diff Documentation | Git [Official]
git-diff – Show changes between commits, commit and working tree, etc | Ubuntu Manpage
使い方
参考:
忘れやすい人のための git diff チートシート – Qiita
オプション
参考:
コミット間におけるファイルの変更を一覧表示する
コミットAからコミットBの間に、修正・追加・削除されたファイルを表示する。
$ git diff --name-status A..B
参考:
Git で commit 間の変更ファイル一覧を出力 – Qiita
変更のあったファイル名を列挙する (内容の差分は表示しない) – Qiita
git で、削除・追加・変更されたファイル一覧を取得するには? – スタック・オーバーフロー
移動/名前を変更したファイルの diff
-M
オプションを使う。
$ git diff -M
もしくは、
$ git diff HEAD:path/to/oldfilename newfilename
参考:
Is there any way have git diff show that a file was moved, instead of removed and added – reddit
How to do a git diff on moved/renamed file? – Stack Overflow
ステージングしたファイルの diff を取る
ワーキングツリーでまだステージングしていない変更を表示する。
$ git diff
git commit
した時にコミットされる変更を表示する。
$ git diff --cached
ワーキングツリーと HEAD
の diff
を表示する。
$ git diff HEAD
参考:
git で add したあとに差分が見たくなった場合 – Qiita
忘れやすい人のための git diff チートシート – Qiita
git diff にもっと詳しくなる | Ren’s blog
Diff staged changes | makandra dev
Diff Staged and Unstaged Files | ShellHacks
How do I show the changes which have been staged? – Stack Overflow
Show git diff on file in staging area – Stack Overflow
what is the difference between –cached and –staged – Stack Overflow
Git diff HEAD vs –staged – Stack Overflow
git status -v / -vv
-v
:ステージングした変更の差分を表示する。(git diff --cached
に相当する)
-vv
:ステージングした変更とステージングしていない変更の差分を表示する。(git diff --cached
+ git diff
に相当する)
パス先頭の文字が表しているエリア
c
:HEAD
i
:ステージングエリア (=インデックス)w
:ワーキングツリー
ステージングの状態
diff --git i/xxx w/xxx
:ステージングされていない変更 (インデックスがHEAD
と同じ状態)diff --git c/xxx i/xxx
:ステージングした変更 (インデックスがワーキングツリーと同じ状態)
参考:
Diff Staged and Unstaged Files | ShellHacks
-v – git-status Documentation | Git [Official]
case insensitive な diff を取る
参考:
How to perform case insensitive diff in Git – Stack Overflow
アルゴリズム
--patience
--histogram
参考:
vimdiff でより賢いアルゴリズム (patience, histogram) を使う – Qiita
Git Source Code Review | Fabien Sanglard’s Website
What is `git diff –patience` for? – Stack Overflow
What’s the difference between `git diff –patience` and `git diff –histogram`? – Stack Overflow
--indent-heuristic オプション
参考:
5分でざっくりわかる git diff の indent Heuristic オプションの仕組み – Qiita
-w / --ignore-all-space オプション
空白文字の違いを全て無視する。
参考:
-w – git-diff Documentation | Git [Official]
--ignore-cr-at-eol オプション
行末のキャリッジリターンを無視する。
参考:
--ignore-cr-at-eol – git-diff Documentation | Git [Official]
--diff-filter オプション
修正があったファイルのみリストを表示する。
$ git diff --diff-filter=M --name-status
フィルターを指定する文字
A
:追加 (Added)C
:複製 (Copied)D
:削除 (Deleted)M
:修正 (Modified)R
:名前変更 (Renamed)T
:種類の変更 (Type changed)U
:未マージ (Unmerged)X
:不明 (Unknown)B
:破損 (pairing Broken)
参考:
--diff-filter – git-diff | Git Documentation [Official]
ディレクトリ毎の統計を表示する
--dirstat
オプションを使う。
$ git diff --dirstat=files
参考:
--dirstat – git-diff | Git Documentation [Official]
特定のディレクトリ以下のみ比較する
--relative
オプションに相対パスを指定する。
$ git diff --name-status --relative=path/to/subdir
参考:
Git で特定のディレクトリ以下の diff を見るときは、relative オプションを使う – Qiita
空白を含むパスを xarg コマンドに渡す
git
コマンドに -z
オプションを指定し、xarg
コマンドに -0
オプションを指定する。
$ git diff --name-only --diff-filter=A -z | xarg -0 git add
参考:
Using git diff with xargs -0, spaces in file names – Super User
git difftool コマンド
参考:
git-diff と git-difftool を混同していた話 | ばうあーろぐ
ファイルやディレクトリを除外する
参考:
git diff で特定のファイル、ディレクトリを除外する方法 (ワンライナー) – Qiita
Excluding folders from git log output | Kashyap Kondamudi
Want to exclude file from “git diff” – Stack Overflow
Exclude a directory from git diff – Stack Overflow
How can I make ‘diff -X’ ignore specific paths and not file names? – Super User
差分がある時のみコミットする
--cached
、--exit-code
、--quiet
オプションを使う。
$ git diff --cached --exit-code --quiet || git commit
参考:
Git の diff コマンドにある --exit-code オプション | Hail2u
リポジトリ外でディレクトリの比較を行う
空白文字の違いを除いて修正があったファイルの diff を表示する。
$ git diff --no-index -w --diff-filter=M dirA/ dirB/
空白文字の違いを除いて修正があったファイルのファイル名と行数を表示する。
$ git diff --no-index -w --diff-filter=M --stat dirA/ dirB/
diff
コマンドと colordiff
を使う。
$ diff -rbu --strip-trailing-cr -x .git \ dirA/ dirB/ \ | colordiff | less -R
ファイル末尾の改行
参考:
\ No newline at end of file の修正 – Qiita
「No newline at end of file」の対処 | Tips というかメモ
行末の「\ No newline at end of file」がうざったい | C_6B4A2B の日記
No newline at end of file というエラー | ハックノート
最後に改行がないファイルが作れない · Issue #152 · vim-jp/issues – GitHub
No Newline at End of File | thoughtbot
git diff thinks the last line of the file has been moved after the end of file? – Stack Overflow
What’s the point in adding a new line to the end of a file? – Unix & Linux Stack Exchange
diff-highlight
git diff
の結果をハイライト表示させる。
Homebrew で Git をインストールした場合、/usr/local/share/git-core/contrib/diff-highlight/diff-highlight
に存在する。
/usr/local/bin
内にシンボリックリンクを張る。
$ sudo ln -s \ /usr/local/share/git-core/contrib/diff-highlight/diff-highlight \ /usr/local/bin/diff-highlight
diff
の結果を diff-highlight
を通して表示する。
$ git -c color.ui=always diff | diff-highlight | less -R
参考:
git/contrib/diff-highlight at master · git/git – GitHub
diff-highlight で Git の diff を見やすくする | work.log
Git の diff を美しく表示するために必要なたった 1 つの設定 | 詩と創作・思索のひろば
git status can’t find diff-highlight – Stack Overflow
patchutils
filterdiff
参考: