generated at
ファイルのステージング (git add)
リポジトリを作成する (git init) でリポジトリというワークスペースを作成しました。続いてはファイルのバージョンを記録していく操作です。 Git操作の基本的な流れ で説明したように、バージョンを記録する「add -> commit -> push」の流れです。
この記事では「add」について説明します。
git add はバージョンを記録する前の準備にあたる操作です。 git add では変更を記録するファイルを選択します。このファイルを選択する操作をステージングと呼びます。

ファイルのステージング
1. リポジトリへ移動
それでは実際に操作してみましょう。リポジトリを作成する (git init) で作成した git-sample にターミナル上で移動しましょう。その後、 $ ls -a を実行すると .git が表示されるはずです。

2. ファイルを作成する
バージョンを管理する対象のファイルを作成します。以下のコマンドを実行してください。
ファイルを作成.sh
$ echo "Hello, Git." > sample.txt $ cat sample.txt
$ cat sample.txt を実行し Hello, Git. と表示されると成功です。

3. ステージング
以下のコマンドを実行してください。 $ git add ${ファイル名1} ${ファイル名2} ... でファイルを選択します。
ステージング.sh
$ git add sample.txt
また一度にすべてのファイルを選択する必要はなく、 $ git add を複数回実行することで複数のファイルをステージングできます。

4. 結果を確認する
結果を確認するには $ git status を実行します。これも Git 操作の一つですが、詳細は別の記事で説明します。
実行後以下のような表示がされればOKです。
実行結果例.sh
$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: sample.txt

本記事ではバージョン管理をする最初の操作である「add」を説明しました。次の記事では変更の確定・保存を行う「commit」を説明します。



【TIPS】複数ファイルを一度に指定する
本記事ではファイル名を直接指定する方法でステージングしました。しかしファイル名を指定せずとも、複数のファイルを一括でステージングしたいときもあります。 $ git add は便利なオプションが複数ありますので紹介します。
本設では以下のディレクトリ構造を想定します。
リポジトリのディレクトリ構造
git-sample ├ .git ├ child <- 今ここにいる │ ├ child_tracked.txt <- 1. 2. 3. の操作でステージング │ └ child_untracked.txt <- 1. 2. の操作でステージング ├ tracked.txt <- 2. 3. の操作でステージング └ untracked.txt <- 2. の操作でステージング
child_tracked.txt , child_untracked.txt , tracked.txt , untracked.txt のすべてに何かしらの変更がされているとします。また、 child_tracked.txt , tracked.txt は追跡されている (tracked) ファイル、 child_untracked.txt , untracked.txt は追跡されていない (untracked) ファイルです。追跡されているファイルとは、一度はそのファイルに対し「git add -> git commit」の操作がされているファイルです。追跡されていないファイルは新規に作成したファイルです。ちなみに本節で操作した sample.txt は untracked なファイルです。
作業ディレクトリは git-sample/child です。

1. git add .
カレントディレクトリ以下にあるファイルをすべてステージングします。現在 git-sample/child にいるので、ステージングされるファイルは child_tracked.txt , child_untracked.txt です。ちなみに「カレントディレクトリすべてを指定している」にすぎませんのでコマンドオプションではありません。
実行結果.sh
$ git add . $ git status On branch master Changes to be committed: <- この文章以下に書かれているファイルがステージングされている (use "git restore --staged <file>..." to unstage) modified: child_tracked.txt new file: child_untracked.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: ../tracked.txt Untracked files: (use "git add <file>..." to include in what will be committed) ../untracked.txt

2. git add -A
リポジトリ内にあるファイルをすべてステージングします。「All」の「A」です。大文字であることに注意してください。 child_tracked.txt , child_untracked.txt , tracked.txt , untracked.txt がステージングされます。
実行結果.sh
$ git add -A $ git status On branch master Changes to be committed: <- この文章以下に書かれているファイルがステージングされている (use "git restore --staged <file>..." to unstage) modified: child_tracked.txt new file: child_untracked.txt modified: ../tracked.txt new file: ../untracked.txt

3. git add -u
リポジトリ内にある追跡されているファイルをすべてステージングします。つまり更新されたファイル(= 新規作成ではないファイル)のみステージングされます。「Update」の「u」です。 child_tracked.txt , tracked.txt がステージングされます。
実行結果.sh
$ git add -u $ git status On branch master Changes to be committed: <- この文章以下に書かれているファイルがステージングされている (use "git restore --staged <file>..." to unstage) modified: child_tracked.txt modified: ../tracked.txt Untracked files: (use "git add <file>..." to include in what will be committed) child_untracked.txt ../untracked.txt