gitコマンドの使い方 ~ git add ~

2020年3月30日git-command

git add コマンド概要

git addを簡単に説明すると「ステージングエリア(インデックス)に追加する」となるのですが、もう少し具体的に理解しておくと、その他のコマンドについても理解が進みやすくなります。

git addは以下の2つの処理を行っています。

  • working-directory(作業ディレクトリ)からrepository(リポジトリ)にファイルを圧縮し、blobオブジェクトとして40文字のhash値を名前として格納します
  • その40文字のhash値をstaging-area(ステージングエリア)のindexファイルに追加(更新)します。

git add コマンドの使い方

git add [file-path] [option]

指定したファイルのblobオブジェクトの配置とstaging-area(ステージングエリア)に反映します。

git add [wild-card] [option]

*.txt」などワイルドカードを指定した場合は、一致するファイル全てを対象にblobオブジェクトの配置とstaging-area(ステージングエリア)に反映します。

git add . [option]

working-directory(作業ディレクトリ)で追加・修正・削除した全てのファイルを対象にblobオブジェクトの配置とstaging-area(ステージングエリア)に反映します。

git add オプション

git addでオプションをあまり利用することがないので、1つだけ紹介します。

-n または –dry-run

実際にステージングエリアに追加などを行わずに借り実行した結果を確認することができます。

$ 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:   file-C.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-AAA.txt

$ git add . -n
add 'file-C.txt'
add 'file-AAA.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:   file-C.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-AAA.txt

借り実行なのでgit add する前後でgit statusの結果が変わっていませんね。

よく使う git add コマンドの利用方法

git statusでステージングされていないファイルを確認し、git add . でstaging-area(ステージングエリア)に反映する

$ 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:   file-C.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file-AAA.txt

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

$ git add .

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

git add の全てのオプションを確認する方法

以下のコマンドを実行するとブラウザでgit addのヘルプページが表示される

git add --help

Gitコマンドの使い方一覧

git-command

Posted by snow