TAKASHI YAMASHINA software engineer

git statusの使い方と便利なオプション

git statusは、作業ディレクトリとステージングエリアの状態を確認するコマンドです。変更されたファイル、ステージングされたファイル、追跡されていないファイルを表示します。

基本的な使い方

現在の状態を確認する

bash
git status

出力例:

plain
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   src/index.js

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:   src/app.js

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        src/new-file.js

短縮形式で表示する

bash
git status -s
# または
git status --short

出力例:

plain
M  src/index.js
 M src/app.js
?? src/new-file.js

よく使うオプション

-s, --short: コンパクトな表示

bash
git status -s

左側の2文字が状態を表します:

plain
M  staged.js        # ステージング済み(Modified)
 M modified.js      # 変更済みだが未ステージング
MM both.js          # ステージング済み + さらに変更あり
A  added.js         # 新規追加(Added)
D  deleted.js       # 削除(Deleted)
R  renamed.js       # リネーム(Renamed)
C  copied.js        # コピー(Copied)
?? untracked.js     # 追跡されていない
!! ignored.js       # 無視されている(--ignoredオプション使用時)

-b, --branch: ブランチ情報を表示

bash
git status -sb

出力例:

plain
## main...origin/main [ahead 1, behind 2]
M  src/index.js
?? src/new-file.js

--ignored: 無視されているファイルも表示

bash
git status --ignored

.gitignoreで無視されているファイルも表示されます。

plain
Ignored files:
  (use "git add -f <file>..." to include in what will be committed)
        node_modules/
        .env
        dist/

--untracked-files: 追跡されていないファイルの表示方法

bash
# すべての未追跡ファイルを表示(デフォルト)
git status --untracked-files=all

# 未追跡ファイルを表示しない
git status --untracked-files=no

# ディレクトリ名のみ表示
git status --untracked-files=normal

-v, --verbose: 詳細な差分を表示

bash
git status -v

ステージングされた変更の詳細な差分(git diff --cachedと同等)も表示されます。

bash
# さらに詳細(ステージング済みと未ステージングの両方)
git status -vv

実践的な使い方

コミット前の確認

bash
# ステージングされたファイルを確認
git status

# 問題なければコミット
git commit -m "Update feature"

作業中断前の確認

bash
# 変更が残っていないか確認
git status

# 変更がある場合はstashまたはコミット
git stash

リモートとの差分を確認

bash
# ブランチ情報を含めて確認
git status -sb

# ahead/behindの状態に応じて対応
git pull  # behindの場合
git push  # aheadの場合

クリーンな状態か確認

bash
git status

# 理想的な出力(何も変更がない状態)
# On branch main
# nothing to commit, working tree clean

短縮形式で素早く確認

bash
# 日常的な確認はこれで十分
git status -sb

出力の読み方

Changes to be committed(ステージング済み)

plain
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file.js

Changes not staged for commit(未ステージング)

plain
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.js

Untracked files(未追跡)

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

ブランチ情報

plain
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

他のコマンドとの組み合わせ

git addの前に確認

bash
# 変更されたファイルを確認
git status -s

# 特定のファイルをステージング
git add src/app.js

# ステージングされたか確認
git status -s

git commitの前に確認

bash
# ステージングされた内容を確認
git status

# 詳細な差分も確認したい場合
git status -v

# コミット
git commit -m "Add new feature"

git pullの前に確認

bash
# ローカルに変更がないか確認
git status

# 変更があればstashまたはコミット
git stash

# pull実行
git pull

git checkoutの前に確認

bash
# 未コミットの変更がないか確認
git status

# 変更がある場合は対処してからブランチ切り替え
git add .
git commit -m "WIP"
git checkout other-branch

statusを使うタイミング

1. 作業開始時

bash
# 前回の作業が残っていないか確認
git status

2. ファイル編集後

bash
# どのファイルを変更したか確認
git status -s

3. git addの後

bash
# 正しいファイルがステージングされたか確認
git status -s

4. git commitの前

bash
# コミットする内容を最終確認
git status -v

5. 作業終了時

bash
# 変更が残っていないか確認
git status

便利なエイリアス

.gitconfigに追加

[alias]
  st = status
  s = status -sb
  ss = status -s

使用例:

bash
git st   # git status
git s    # git status -sb(ブランチ情報付き短縮形式)
git ss   # git status -s(短縮形式のみ)

よくある状況と対処

大量のuntracked filesが表示される

bash
# .gitignoreに追加すべきファイルを確認
git status --ignored

# .gitignoreを更新
echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore
git add .gitignore
git commit -m "Update .gitignore"

変更が多すぎて見づらい

bash
# 短縮形式で確認
git status -s

# さらに、未追跡ファイルは非表示
git status -s --untracked-files=no

ステージングと未ステージングが混在

bash
# 短縮形式で状態を確認
git status -s

# 左の列:ステージング状態
# 右の列:作業ディレクトリの状態
MM file.js  # ステージング済み + さらに変更あり

対処方法:

bash
# すべての変更をステージング
git add file.js

# または、ステージング済みの内容だけコミット
git commit -m "Partial changes"

リモートとの差分がわからない

bash
# ブランチ情報を含めて確認
git status -sb

# 詳細を確認
git log origin/main..HEAD      # ローカルにある追加コミット
git log HEAD..origin/main      # リモートにある追加コミット

statusが教えてくれること

1. 何が変更されたか

2. どこに変更があるか

3. 次に何をすべきか

plain
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)

Gitが親切に次のアクションを提案してくれます。

4. リモートとの関係

statusを使う習慣

コマンドの前後で確認

bash
git status      # 前
git add .       # 実行
git status      # 後(確認)

定期的に確認

作業中は15分に1回程度git statusを実行する習慣をつけると:

ブランチ切り替え前に必ず確認

bash
git status      # 未コミットの変更がないか確認
git checkout feature-branch

未コミットの変更があるとブランチ切り替えに失敗することがあります。