1
0

ステージされている変更/されていない変更の閲覧 "git diff"

Posted at

記事の目的

  • git diff コマンドについて記載
  • "git status"より詳細に知りたい時に使う
  • Pro Gitを基に記事を作成しました。
  • 後ほど記事の改修しやすいように元の記事から分離しました。

本文

git status ではよくわからない時に git diff を使う。

言い換えると、どのファイルが変更されたのかだけではなく、実際にどのように変わったのが知りたい場合に使う。
追加したり削除したりした正確な行をパッチ形式で表示する。

最もよく使う場面

二つの問いに答えるとき使う
・変更したけどまだステージしていない変更は?
・コミット対象としてステージした変更は?

先ほどの続きで、READMEファイルを編集してステージし、一方CONTRIBUTING.mdファイルは編集だけしてステージしない状態とする。

  • git status を実行すると次の結果になる。
$ git status
  On branch master
  Changes to be committed:
    (use "git reset HEAD <file>..." to unstage)
      modified:   README
  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:   CONTRIBUTING.md

  • 変更したけれどもまだステージしていない内容を見るには、引数なしで git diff を実行する。
$ git diff
  diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
  index 8ebb991..643e24f 100644
  --- a/CONTRIBUTING.md
  +++ b/CONTRIBUTING.md
  @@ -65,7 +65,8 @@ branch directly, things can get messy.
   Please include a nice description of your changes when you submit your
  PR;
   if we have to read the whole diff to figure out why you're contributing
   in the first place, you're less likely to get feedback and have your
  change
  -merged in.
  +merged in. Also, split your changes into comprehensive chunks if your
  patch is
  +longer than a dozen lines.
   If you are starting to work on a particular area, feel free to submit a
  PR
   that highlights your work in progress (and note in the PR title that it's

コマンド: git diff
要望:変更したけれどもまだステージしていない内容を見たい。
仕様:このコマンドは、作業ディレクトリの内容とステージングエリアの内容を比較します。
結果:変更した内容のうちまだステージされていないものを知ることができる。

  • 次のコミットに含めるべくステージされた内容を知りたい場合 git dif --stagedを使用する。
$ git diff --staged
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+My Project

コマンド: git diff --staged
要望:次のコミットに含めるべくステージされた内容を知りたい
仕様:このコマンドは、ステージされている変更と直近のコミットの内容を比較します。
注意:直近のコミット以降の全ての変更を表示するわけではない。あくまでもステージされていない変更だけの表示。変更内容を全てステージしてしまえば何も出力しなくなる。

  • 別例:CONTRIBUTING.md ファイルをいったんステージした後に編集してみる。
    git diff を使用すると、ステージされたファイルの変更とまだステージされていないファイルの変更を見ることができる。
以下のような状態とする。
$ git add CONTRIBUTING.md
$ echo '# test line' >> CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   CONTRIBUTING.md

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:   CONTRIBUTING.md

git diff を使うことで、まだステージされていない内容を知ることができる。

$ git diff
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 643e24f..87f08c8 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -119,3 +119,4 @@ at the
 ## Starter Projects

 See our [projects
list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).
+# test line

git diff --cached を使うと、これまでにステージした内容を知ることができる。(--staged--cached は同義です)。

$ git diff --cached
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index 8ebb991..643e24f 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -65,7 +65,8 @@ branch directly, things can get messy.
 Please include a nice description of your changes when you submit your
PR;
 if we have to read the whole diff to figure out why you're contributing
 in the first place, you're less likely to get feedback and have your
change
-merged in.
+merged in. Also, split your changes into comprehensive chunks if your
patch is
+longer than a dozen lines.

 If you are starting to work on a particular area, feel free to submit a
PR
that highlights your work in progress (and note in the PR title that it's

参考文献

Gitについてのリンク

1
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
1
0