gitのコマンド確認 init status diff log

git init

まずはgit initコマンドを実行。空のgitリポジトリが作成されます。

% git init


中身を確認してみると、.gitという隠れフォルダが作成されています。

% ls -a
.       ..      .git


.gitファイルの中身を見てみると

% ls .git/
HEAD        config      description hooks       info        objects     refs
  • objects:リポジトリの本体。この中に圧縮ファイルやツリーファイルが保存されていく。

  • config:gitの設定ファイル。



git status

上の内容をgit add.します。 次にgit statusで内容を確認すると

% git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   index.html

するとcommitすべき変更があるよと表示されます。 それではcommitしてみましょう。commitが終わると

% git status
On branch master
nothing to commit, working tree clean

変更されたのでワークツリーとcommitの間に変更差分はないよとなります。このようにgit addやgit commitしたときに、どのファイルに対して行なっているか確認するくせが必要ですね。



変更差分が確認できるgit diff

index.htmlに以下を記載しgit diffで確認すると

<p>夏目友人帳</p>
% git diff
diff --git a/index.html b/index.html
index 04640d6..3f061a9 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
 <p>インデックス</p>
-<p>git status</p>
\ No newline at end of file
+<p>git status</p>
+<p>夏目友人帳</p>
\ No newline at end of file

+と書かれているところが変更部分になります。

それではgit add.コマンドを打ってから再度git diffコマンドで確認すると何も表示されなくなりました。git diffはワークツリーとステージ間の変更差分のみ表示してくれます。

% git diff


次にgit diff --stagedを入力するとステージとコミットの差分の変更を確認してくれます。

% git diff --staged
diff --git a/index.html b/index.html
index 04640d6..3f061a9 100644
--- a/index.html
+++ b/index.html
@@ -1,2 +1,3 @@
 <p>インデックス</p>
-<p>git status</p>
\ No newline at end of file
+<p>git status</p>
+<p>夏目友人帳</p>
\ No newline at end of file



変更履歴を確認するgit log

git logをすると今までのcommitの変更履歴を確認することができます。上から最新順に表示されます。

% git log

commit 69fcc617a5ea4fc79823927c59d4b0219d371839 (HEAD -> master)
Author: 
Date:   Wed Nov 17 19:16:15 2021 +0900

    git_diff(コミット名)

commit a0ba9495c735ec7e6191b502ddfaefc0729efbc4
Author:
Date:   Wed Nov 17 19:01:51 2021 +0900

    git_statusコマンド追記