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

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設定git config ~Gitリポジトリの設定~
初期化git init ~ローカルリポジトリの構築~
ログ&設定値確認git log ~commitログの履歴確認~
git status ~作業ディレクトリとステージングエリアの更新状態確認~
git diff ~commitやファイルの差分確認~
git show ~commitの内容確認~
git remote ~リモート追跡ブランチのリモートリポジトリの設定確認~
git reflog ~HEADやブランチの移動履歴の確認と整理~
ステージングエリアの操作git add ~ステージングエリア(インデックス)にファイル追加~
git ls-files ~ステージングエリア(インデックス)のファイル一覧表示~
git rm ~ステージングエリア(インデックス)のファイル削除~
ローカルリポジトリの操作git commit ~blobやtreeなどをまとめたcommitを作成~
git merge ~指定したブランチの内容を取り込み新しいcommitを作成~
git rebase ~commitの履歴の整理~
git cherry-pic ~特定のcommitの変更内容だけを取り込む~
git cat-file ~リポジトリのオブジェクト(commit,tree,blob)の内容表示~
git revert ~指定したcommitを取り消すcommitを作成~
commitのエイリアスなどgit branch ~commitツリーの枝(ブランチ)を作成~
git tag ~リリースなどのタイミングで特定のcommitに名前を付ける~
git stash ~commitを作成する前の変更内容を一時保存~
commitの内容をステージングエリアや作業ディレクトリに反映git checkout ~commitをステージングエリアと作業ディレクトリに展開~
git reset ~HEADの位置やステージングエリアと作業ディレクトリの内容を変更~
リモートリポジトリとのやり取りgit clone ~リモートリポジトリからローカルリポジトリ作成~
git fetch ~リモートリポジトリのブランチをローカルリポジトリに反映~
git pull ~git fetchとgit mergeを同時に行う~
git push ~ローカルリポジトリのブランチをリモートリポジトリに反映~