0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Git] 動作を試す 実行例9:diffで変更の差異を見る

Last updated at Posted at 2023-04-29

Gitの動作を理解するために、Gitのコマンドを実際に試して、結果を見てみました。

1つの記事内で一連のGitコマンドが完結しているので、これら一連のコマンドを順に実行させて結果を見ることで、一連のGitの動作を実際に体感でき、一通り独習することが可能です。

※前回記事のリポジトリ状態からの続きになっています。

前回記事へ 目次へ:Git関連記事のまとめページ 次回記事へ

実行例

忘れやすい人のための git diff チートシート
https://qiita.com/shibukk/items/8c9362a5bd399b9c56be

(※始めにコミットSHA値を調べる)
git log --oneline
↓
結果: 
94996a0 (HEAD -> master) message4
20bbab7 message3
00cad71 message2
4ace194 message1

※message3~message4の比較
git diff 20bbab7
↓
結果: 
diff --git a/test1.txt b/test1.txt
index 2cb4452..bb7eb1a 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1,3 +1,4 @@
 Sample-Added-11
 Sample-Added-12
 Sample-Added-13
+Sample-Added-14
diff --git a/test4.txt b/test4.txt
new file mode 100644
index 0000000..d42c59e
--- /dev/null
+++ b/test4.txt
@@ -0,0 +1 @@
+Sample-Added-41

※git showと同じような表示となる

-----

git diff 20bbab7 --stat
↓
結果: 
 test1.txt | 1 +
 test4.txt | 1 +
 2 files changed, 2 insertions(+)

git diff 20bbab7 --numstat
↓
結果: 
1       0       test1.txt
1       0       test4.txt

git diff 20bbab7 --shortstat
↓
結果: 
 2 files changed, 2 insertions(+)

git diff 20bbab7 --name-status
↓
結果: 
M       test1.txt
A       test4.txt

-----

git diff 20bbab7 test1.txt
若しくは、
git diff 20bbab7 "test1.txt"
git diff 20bbab7 -- test1.txt
↓
結果: 
 test1.txt | 1 +
diff --git a/test1.txt b/test1.txt
index 2cb4452..bb7eb1a 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1,3 +1,4 @@
 Sample-Added-11
 Sample-Added-12
 Sample-Added-13
+Sample-Added-14

-----

git diff 20bbab7 -U1 test1.txt
↓
結果: 
diff --git a/test1.txt b/test1.txt
index 2cb4452..bb7eb1a 100644
--- a/test1.txt
+++ b/test1.txt
@@ -3 +3,2 @@ Sample-Added-12
 Sample-Added-13
+Sample-Added-14

-----

※message1~message3の比較
git diff 4ace194 20bbab7
若しくは、
git diff 4ace194..20bbab7
↓
結果: 
diff --git a/test1.txt b/test1.txt
index 046a699..2cb4452 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1 +1,3 @@
 Sample-Added-11
+Sample-Added-12
+Sample-Added-13
diff --git a/test2.txt b/test2.txt
new file mode 100644
index 0000000..1facb47
--- /dev/null
+++ b/test2.txt
@@ -0,0 +1 @@
+Sample-Added-**21

-----

※改行コードや空白を無視する場合
git diff -w

環境

Windows 10、PortableGit-2.40.0-64-bitを使用、全てローカルPC上で実施、GitHub等は不使用。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?