Atsushi2022の日記

データエンジニアリングに関連する記事を投稿してます

ローカルにおけるGitコマンド

アーキテクチャ

全体的なアーキテクチャを理解しよう。

まずローカルPCとクラウドに分類される。ローカルPCのドキュメントをクラウドに配置する(push)、あるいはクラウドから取ってくる(pull)。

ローカルPC内では、データがワークツリー、ステージ、リポジトリのいずれか、あるいはすべてに保管される。

ワークツリーで編集した内容をgit addでステージに配置する。さらにgit commitでステージからリポジトリに配置する。

ひとまずcommitしてみる

ステージにtest.txtを入れる。

git add test.txt

git add .とすることで、フォルダ(ワークツリー)にあるすべてのファイルをaddすることもできる。

ステージに入れたので、さらにcommitしてリポジトリに移したい。だが、次のようにエラーとなる。

> git commit -m "1st commit"
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

Eメールとユーザー名を事前に登録しておかなければならない。

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

再度Commitすると今度はうまくいった。

> git commit -m "1st commit"
[master (root-commit) 54ed5fb] 1st commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

branchを作成してみる

ここでbranchを確認してみる。masterブランチのみ存在するのが確認できる。 初Commit前に、git branchコマンドでbranchを確認しても何も表示されないことに注意。Commitして初めてbranchが確認できるようになる。

> git branch
* master

試しにdevブランチを作成してみる。アスタリスクが現在のブランチを指している。devブランチを作成しただけでは、devブランチに移らないので注意。

> git branch dev
> git branch
  dev
* master

branchをスイッチするには、git checkoutコマンドを使う。devブランチに移っていることがわかる。

> git checkout dev
Switched to branch 'dev'
> git branch
* dev
  master

statusを確認してみる

現在、test.txtにはnumber1という文字列が入っている。

> cat .\test.txt
number1

test.txtにはnumber2という文字列を追加する。

> cat .\test.txt
number1
number2

git statusコマンドにより、リポジトリとステージ間で差異があるファイルをリストアップできる。modified: test.txtと表示されているが、これは「ファイルが変更されているけれど、まだステージに追加されていませんよ」という意味。

> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

次に、db.txtをaddしてみる。

> git add db.txt

再度、git statusコマンドを実行すると、new file: db.txtというのが追加で表示されていることがわかる。これは「新しくファイルがステージに追加されたけど、まだコミットされてませんよ」という意味。

> git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   db.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

これでコミットしてstatusを確認すると、test.txtはステージに追加してなかったので、modified: test.txtという表示が消えてない。

> git commit -m "New file: db.text"
[master 1428b7d] New file: db.text
 1 file changed, 1 insertion(+)
 create mode 100644 db.txt
> git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

git addでtest.txtをステージに追加してからコミットすると、nothing to commit, working tree cleanとなり、すべてコミットされていることが確認できる。

> git add test.txt
> git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   test.txt

> git commit -m "Add line in test.txt"
[master 338041d] Add line in test.txt
 1 file changed, 2 insertions(+), 1 deletion(-)
> git status
On branch master
nothing to commit, working tree clean

diffを確認してみる

git diffではステージとワークツリー間の差分を確認する。一方、git diff --stagedではステージとリポジトリ間の差分を確認する。

test2.txtというファイルがあり、中身には「1」が入っている。ワークツリーとステージ間、ステージとリポジトリ間で差分がない状態になっている。

> cat test2.txt                                                              
1                                                                                                                                                                                                                                               > git diff                                                                   
> git diff --staged
>

test2.txtに「2」を追加し、差分を確認する。git diff によりワークツリーとステージ間には差分があり、一方git diff --stagedによりステージとリポジトリ間で差分がない、ということがわかる。

> vim test2.txt                                                              
> cat test2.txt
1
2

> git diff                                                                   
diff --git a/test2.txt b/test2.txt
index d474e1b..c47213d 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1,2 +1,3 @@
 1
+2

> git diff --staged
>

git addでtest2.txt をステージに追加する。すると、ワークツリーとステージ間では差分がなくなり、ステージとリポジトリ間で差分がある状態になる。git diffとgit diff --stagedにより、それが確認できる。

> git add test2.txt                                                          
> git diff                                                                   
> git diff --staged
diff --git a/test2.txt b/test2.txt
index d474e1b..c47213d 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1,2 +1,3 @@
 1
+2

resoreしてみる

git restore --stagedではリポジトリを元にステージを復元する。git restoreではステージを元にワークツリーを復元する。

実際にやってみる。

まず、ワークツリーとステージとリポジトリ間で差分がない状態になっている。

> cat test2.txt                                                              
1 
> git diff                                                               
> git diff --staged
>

test2.txt に「2」を追加する。ワークツリーとステージ間で差分が発生する。

> vim test2.txt
> cat test2.txt                                                              
1
2
> git diff                                                                   
diff --git a/test2.txt b/test2.txt
index d474e1b..c47213d 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1,2 +1,3 @@
 1
+2
> git diff --staged
>

git restoreにより、ステージを元にワークツリーを復元する。中身が「1」に戻っている。

> git restore test2.txt
> cat test2.txt                                                              
1

再度test2.txt に「2」を追加し、さらにステージに追加する。ステージとリポジトリ間で差分が発生する。

> vim test2.txt
> cat test2.txt                                                              
1
2
> git add test2.txt
> git diff
> git diff --staged
diff --git a/test2.txt b/test2.txt
index d474e1b..c47213d 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1,2 +1,3 @@
 1
+2

git restore --stagedにより、リポジトリを元にステージを復元したため、ステージとリポジトリ間で差分が解消する。リポジトリを元にステージが復元されたことにより、ワークツリーとステージ間で差分が発生している。

> git restore --staged test2.txt
> git diff --staged
> git diff
diff --git a/test2.txt b/test2.txt
index d474e1b..c47213d 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1,2 +1,3 @@
 1
+2