Gitの使い方が解る Staging Areaの操作
- 2021.02.14
- Git

前回、Gitの事始めとして色々説明してきましたが、今回は Git の Staging Area (ステージングエリア)の操作について説明します。
最初の方に簡単な操作を説明して、後半で深く掘り下げていきます。簡単な使い方だけ知りたい方は後半は読み飛ばしてください。
git status
前回も説明したコマンドとなりますが、Staging Areaの状態を確認するには git status
を実行します。
git status
で表示された内容の見方は後述します。
git add
Working Directory(作業ディレクトリ)にあるファイルを Staging Areaにステージするためには git add
を実行します。
一つのファイルを git add
するには次のようにファイル名を指定することでできました。
bash-3.2$ git add sample.txt
複数のファイルを git add
するにはどうしたら良いでしょうか。
次のように複数のファイルを指定することもできます。
bash-3.2$ git add sample2.txt sample3.txt
また、このように指定することもできます。
bash-3.2$ git add sample?.txt
ワイルドカード指定もできます。
bash-3.2$ git add *.txt
もう少し踏み込んだ内容については後述します。
git rm
ファイルを削除した場合はどうなるでしょうか。その場合は git rm
を使います。
ただ、ファイル削除といっても次の4パターンがあるので、それぞれ見ていきたいと思います。
Staging Areaにない状態でファイルを削除した場合
要するにファイルを作って git add
も git commit
もしていないファイルを消した場合です。
この場合は Git側は特に何もする必要はありません。
Staging Areaにあってリポジトリにはない状態でファイルを削除した場合
まだコミット前の状態で、Staging Area にあったファイルをWorking Directoryで削除した場合です。
この場合は git rm
で Staging Area から削除してあげる必要があります。
サンプルを元に説明します。
bash-3.2$ echo delete sample > sample2.txt
bash-3.2$ git add sample2.txt
bash-3.2$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: sample2.txt
bash-3.2$ rm sample2.txt
bash-3.2$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: sample2.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: sample2.txt
bash-3.2$ git rm sample2.txt
rm 'sample2.txt'
bash-3.2$ git status
On branch master
nothing to commit, working tree clean
新しく作った sample2.txt を git add
で Staging Area にステージしています。
その後、sample2.txt を Working Directory から削除してしまったので、
deleted: sample2.txt
となっています。
そのため、git rm
で Staging Area から削除しているのです。
すでにリポジトリにあったファイルを削除した場合
この場合は、次のコミット時にgit repositoryからファイルを削除する必要があるため、deleted という状態でステージする必要があります。
分かりにくいかもしれないのでサンプルで説明します。
bash-3.2$ rm sample.txt
bash-3.2$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: sample.txt
no changes added to commit (use "git add" and/or "git commit -a")
bash-3.2$ git rm sample.txt
rm 'sample.txt'
bash-3.2$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: sample.txt
すでにリポジトリにコミット済みの sample.txt を Working Directory 上で削除します。
この状態で git status
するとステージされていないけど、sample.txt が deleted になっていると表示されます。
このままコミットしてもリポジトリから削除されないので、deletedの状態であることを git rm
コマンドでステージします。(git add
でも同じことができます。)
そうすると、次回コミットしたときにリポジトリから sample.txt は削除されます。
Working Direcotry にはファイルを残したいけど、Staging Area からはファイルを削除したい場合
この場合は、 –cached オプションを使用します。
bash-3.2$ ls
sample.txt sample2.txt
bash-3.2$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
sample2.txt
nothing added to commit but untracked files present (use "git add" to track)
bash-3.2$ git add .
bash-3.2$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: sample2.txt
bash-3.2$ git rm --cached sample2.txt
rm 'sample2.txt'
bash-3.2$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
sample2.txt
nothing added to commit but untracked files present (use "git add" to track)
bash-3.2$ ls
sample.txt sample2.txt
まずは sample2.txt をステージします。ですが、やっぱり Staging Area から消したいけど、ファイル自体は残したいので、次のように git rm
に –cached オプションをつけて実行します。
bash-3.2$ git rm --cached sample2.txt
git mv
ファイルを移動したりリネームしたい場合はどうしたらいいでしょうか。
bash-3.2$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sample.txt
Staging Area にある sample.txt を Working Directory 上でリネームしてみます。
bash-3.2$ mv sample.txt sample2.txt
bash-3.2$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sample.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: sample.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
sample2.txt
複雑な状態になってしまいました。
この状態がどんな状態であるかは、後述する git statusの見方 を参照してください。
こうなってしまった場合は、次のように Staging Area を変更してやる必要があります。
bash-3.2$ git rm sample.txt
rm 'sample.txt'
bash-3.2$ git add sample2.txt
bash-3.2$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sample2.txt
sample.txtがリネームされたときに Staging Area では、sample.txtが削除されて、sample2.txt という新しいファイルが作成されたと認識してしまったためです。
ファイルをリネームするだけでこの操作をするのは大変です。そこで git mv
を使います。
bash-3.2$ git mv sample2.txt sample.txt
bash-3.2$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: sample.txt
bash-3.2$ ls
sample.txt
Staging Area も Working Directory も一緒にリネームされました。これなら簡単です。
git status の見方
TrackedとUntracked

Git には Working Dairectory と Saging Area と Repository の3つのエリアがあることを説明しましたが、さらにファイルには Tracked(追跡されている)ファイルとUntracked(追跡されていない)ファイルの2種類存在します。

Working Directory に最初ファイルを作成するとまだ Untracked の状態のファイルとなります。

この後、Staging Area にステージした時点で Tracked 状態となります。
いろいろなStatusの確認
Changes to be committed

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: sample2.txt
Changes to be commited
は Staging Area にいる状態です。
new file と表示されているため、Staging Area に新しく追加されたファイルということです。
Changes to be committed (modified:)

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: sample.txt
modified はすでに Repository にあるファイルが変更された状態で Staging Area にある場合に表示されるステータスです。
Untracked files

Untracked files:
(use "git add <file>..." to include in what will be committed)
sample2.txt
Untracked files
はまだ追跡されていないファイルで一度もStaging Area にステージしたことないファイルです。
Changes not staged for commit

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: sample.txt
Changes not staged for commit
は Repository にあるファイルをWorking Directory で編集し、ステージした場合に表示されます。
この状態で、コミットすると変更した内容が Repository に反映されます。
git addするときの複数ファイル指定
git add するときに複数ファイルを指定する方法を先ほど説明しましたが、ワイルドカード指定した場合は、glob ファイルパターンによってファイルが指定されます。
git add *.txt
カレントディレクトリの .txt がつくファイルをすべて指定
git add dir1/*.txt
dir1ディレクトリ内の.txtがつくファイルをすべて指定
git add *
アスタリスクのみ。カレントディレクトリ内のファイルとディレクトリをすべて指定(.ドットで始まるファイルやディレクトリは除外されます)
それでは、.ドットで始まるファイルやディレクトリも一緒に git add
したい場合はどうしたらいいでしょうか。
git add .
. (ドット) 指定でいけます。
これら2つの方法では指定できない対象があります。
それは Working Directory から削除されたファイルです。
削除されたファイルも一緒に Staging Area にステージしたい場合、削除したファイルだけ git rm
すればいいのですが、次のオプションでまとめてステージすることができます。
git add -A
もう一つのパターンも説明しておきます。
Untrackedのファイルはステージしたくない場合。この場合は、次のオプションで git add
すればいけます。
git add -u
これらの内容を下の表にまとめました。

だいぶ長くなってしまいましたが、これだけ知っていれば Staging Area の操作は問題ないでしょう。
-
前の記事
Gitの使い方が解る Gitとは 2021.02.11
-
次の記事
Gitの使い方が解る git commitとCommitコメント 2021.10.31