はじめに

Gitには数多くのコマンドとオプションがあり、すべてを暗記するのは困難です。このチートシートでは、Gitの基本操作から応用テクニックまで、実務で頻繁に使用するコマンドを一覧形式でまとめました。

日常の開発作業やトラブル対応時に素早く参照できるリファレンスとして活用してください。各コマンドには用途と代表的なオプションを記載しています。

実行環境と前提条件

本チートシートの内容は、以下の環境での使用を想定しています。

項目 要件
Git 2.40以上
OS Windows 10/11、macOS 12以上、Ubuntu 22.04以上
ターミナル コマンドプロンプト、PowerShell、Terminal.app、bash等
エディタ VS Code推奨

前提条件として、以下の知識があることを想定しています。

  • コマンドライン操作の基礎知識(cdls/dirmkdir等)
  • テキストエディタの基本操作

基本操作コマンド

リポジトリの初期化とクローン

コマンド 説明
git init 現在のディレクトリをGitリポジトリとして初期化
git init --bare ベアリポジトリ(作業ディレクトリなし)を作成
git clone <url> リモートリポジトリを複製
git clone --depth 1 <url> 最新コミットのみを取得(シャロークローン)
git clone --branch <branch> <url> 特定のブランチを指定してクローン
1
2
3
4
5
6
7
8
# 基本的な初期化
git init

# GitHubからリポジトリをクローン
git clone https://github.com/user/repo.git

# シャロークローンで高速にクローン
git clone --depth 1 https://github.com/user/repo.git

ステージングとコミット

コマンド 説明
git add <file> 指定ファイルをステージング
git add . 現在のディレクトリ以下のすべての変更をステージング
git add -A リポジトリ全体のすべての変更をステージング
git add -p 対話的に変更をステージング(パッチモード)
git commit -m "<message>" メッセージを指定してコミット
git commit -am "<message>" ステージングとコミットを同時に実行(追跡済みファイルのみ)
git commit --amend 直前のコミットを修正
git commit --amend --no-edit メッセージを変更せずに直前のコミットを修正
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# ファイルをステージング
git add src/main.js

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

# 対話的にステージング
git add -p

# コミット
git commit -m "feat: 新機能を追加"

# 直前のコミットを修正
git commit --amend -m "feat: 新機能を追加(修正)"

状態確認

コマンド 説明
git status 作業ディレクトリとステージングの状態を表示
git status -s 短縮形式で状態を表示
git status --porcelain スクリプト向けの出力形式
1
2
3
4
5
# 状態確認
git status

# 短縮形式
git status -s

ブランチ操作コマンド

ブランチの作成と切り替え

コマンド 説明
git branch ローカルブランチ一覧を表示
git branch -a ローカルとリモートのブランチ一覧を表示
git branch -r リモートブランチ一覧を表示
git branch <branch> 新しいブランチを作成(切り替えなし)
git branch -d <branch> マージ済みブランチを削除
git branch -D <branch> ブランチを強制削除
git branch -m <old> <new> ブランチ名を変更
git switch <branch> ブランチを切り替え
git switch -c <branch> 新しいブランチを作成して切り替え
git switch - 直前のブランチに切り替え
git checkout <branch> ブランチを切り替え(従来のコマンド)
git checkout -b <branch> 新しいブランチを作成して切り替え(従来のコマンド)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ブランチ一覧
git branch -a

# 新しいブランチを作成して切り替え
git switch -c feature/new-feature

# 直前のブランチに戻る
git switch -

# ブランチを削除
git branch -d feature/old-feature

マージとリベース

コマンド 説明
git merge <branch> 指定ブランチを現在のブランチにマージ
git merge --no-ff <branch> Fast-forwardを無効にしてマージコミットを作成
git merge --squash <branch> 複数コミットを1つにまとめてマージ
git merge --abort マージを中止して元の状態に戻す
git rebase <branch> 現在のブランチを指定ブランチ上にリベース
git rebase -i <commit> 対話的リベース(コミットの編集・並べ替え・統合)
git rebase --continue コンフリクト解消後にリベースを続行
git rebase --abort リベースを中止して元の状態に戻す
git rebase --skip 現在のコミットをスキップしてリベースを続行
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# ブランチをマージ
git merge feature/new-feature

# マージコミットを必ず作成
git merge --no-ff feature/new-feature

# 対話的リベース(直近5コミットを編集)
git rebase -i HEAD~5

# mainブランチ上にリベース
git rebase main

リモート操作コマンド

リモートリポジトリの管理

コマンド 説明
git remote リモートリポジトリ一覧を表示
git remote -v リモートのURLを含めて表示
git remote add <name> <url> リモートリポジトリを追加
git remote remove <name> リモートリポジトリを削除
git remote rename <old> <new> リモート名を変更
git remote set-url <name> <url> リモートのURLを変更
git remote show <name> リモートの詳細情報を表示
1
2
3
4
5
6
7
8
# リモート一覧(URLを含む)
git remote -v

# リモートを追加
git remote add origin https://github.com/user/repo.git

# URLを変更(HTTPSからSSHに変更)
git remote set-url origin git@github.com:user/repo.git

プッシュとプル

コマンド 説明
git push デフォルトリモートにプッシュ
git push -u origin <branch> 上流ブランチを設定してプッシュ
git push --force 強制プッシュ(危険)
git push --force-with-lease 安全な強制プッシュ(推奨)
git push --tags すべてのタグをプッシュ
git push origin --delete <branch> リモートブランチを削除
git fetch リモートの変更を取得(マージしない)
git fetch --all すべてのリモートから取得
git fetch --prune 削除されたリモートブランチを反映
git pull フェッチとマージを実行
git pull --rebase フェッチとリベースを実行
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 上流ブランチを設定してプッシュ
git push -u origin main

# 安全な強制プッシュ
git push --force-with-lease

# リモートブランチを削除
git push origin --delete feature/old-feature

# リベースを使ってプル
git pull --rebase origin main

履歴確認コマンド

ログの表示

コマンド 説明
git log コミット履歴を表示
git log --oneline 1行形式で表示
git log --graph グラフ形式で表示
git log --oneline --graph --all 全ブランチをグラフで表示
git log -n <num> 直近n件のコミットを表示
git log -p 差分を含めて表示
git log --stat 変更ファイルの統計を表示
git log --author="<name>" 特定の作成者のコミットを表示
git log --since="2024-01-01" 指定日以降のコミットを表示
git log --grep="<pattern>" メッセージで検索
git log -- <file> 特定ファイルの履歴を表示
git log -S "<string>" 特定の文字列を含む変更を検索
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# グラフ形式で全ブランチを表示
git log --oneline --graph --all

# 直近10件を差分付きで表示
git log -10 -p

# 特定ファイルの履歴
git log --oneline -- src/main.js

# 文字列を含む変更を検索
git log -S "functionName"

差分の確認

コマンド 説明
git diff 作業ディレクトリとステージングの差分を表示
git diff --staged ステージングとHEADの差分を表示
git diff <commit> HEADと指定コミットの差分を表示
git diff <commit1> <commit2> 2つのコミット間の差分を表示
git diff <branch1> <branch2> 2つのブランチ間の差分を表示
git diff --stat 変更ファイルの統計のみを表示
git diff --name-only 変更ファイル名のみを表示
git diff -- <file> 特定ファイルの差分を表示
1
2
3
4
5
6
7
8
# ステージング済みの差分を確認
git diff --staged

# ブランチ間の差分
git diff main..feature/new-feature

# 変更ファイル名のみ表示
git diff --name-only HEAD~3

コミットの詳細表示

コマンド 説明
git show 最新コミットの詳細を表示
git show <commit> 指定コミットの詳細を表示
git show <commit>:<file> 特定コミット時点のファイル内容を表示
git show --stat <commit> 変更ファイルの統計を表示
1
2
3
4
5
# コミットの詳細を表示
git show abc1234

# 特定コミット時点のファイル内容
git show HEAD~3:src/main.js

変更履歴の追跡

コマンド 説明
git blame <file> 各行の最終変更コミットを表示
git blame -L <start>,<end> <file> 指定行範囲の変更履歴を表示
git blame -w <file> 空白の変更を無視
git blame -M <file> 移動された行を検出
1
2
3
4
5
# ファイルの変更履歴
git blame src/main.js

# 行範囲を指定
git blame -L 10,20 src/main.js

変更の取り消しコマンド

作業ディレクトリの変更を取り消す

コマンド 説明
git restore <file> ファイルの変更を取り消し(ステージング前)
git restore . すべての変更を取り消し
git restore --staged <file> ステージングを取り消し(変更は保持)
git restore --source=<commit> <file> 特定コミットの状態に復元
git checkout -- <file> ファイルの変更を取り消し(従来のコマンド)
git clean -n 削除対象の未追跡ファイルを確認
git clean -f 未追跡ファイルを削除
git clean -fd 未追跡ファイルとディレクトリを削除
1
2
3
4
5
6
7
8
9
# ファイルの変更を取り消し
git restore src/main.js

# ステージングを取り消し
git restore --staged src/main.js

# 未追跡ファイルを確認して削除
git clean -n
git clean -f

コミットの取り消し

コマンド 説明
git reset --soft HEAD~1 コミットを取り消し(変更はステージング済みのまま)
git reset --mixed HEAD~1 コミットとステージングを取り消し(変更は保持)
git reset --hard HEAD~1 コミットと変更をすべて取り消し(危険)
git revert <commit> 指定コミットを打ち消す新しいコミットを作成
git revert --no-commit <commit> revertの変更をコミットせずに適用
git revert -m 1 <merge-commit> マージコミットをrevert
1
2
3
4
5
6
7
8
# 直前のコミットを取り消し(変更は保持)
git reset --soft HEAD~1

# コミットを打ち消すrevertコミットを作成
git revert abc1234

# マージコミットをrevert
git revert -m 1 abc1234

一時退避コマンド

スタッシュ操作

コマンド 説明
git stash 作業中の変更を一時退避
git stash -u 未追跡ファイルも含めて退避
git stash -m "<message>" メッセージを付けて退避
git stash list 退避した変更の一覧を表示
git stash show 最新のstashの内容を表示
git stash show -p 最新のstashの差分を表示
git stash pop 最新のstashを適用して削除
git stash apply 最新のstashを適用(削除しない)
git stash apply stash@{n} 指定したstashを適用
git stash drop 最新のstashを削除
git stash drop stash@{n} 指定したstashを削除
git stash clear すべてのstashを削除
git stash branch <branch> stashから新しいブランチを作成
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 変更を一時退避
git stash -m "WIP: 機能開発中"

# 退避した変更を確認
git stash list

# 退避した変更を適用
git stash pop

# 特定のstashを適用
git stash apply stash@{1}

タグ操作コマンド

タグの管理

コマンド 説明
git tag タグ一覧を表示
git tag -l "<pattern>" パターンに一致するタグを表示
git tag <tagname> 軽量タグを作成
git tag -a <tagname> -m "<message>" 注釈付きタグを作成
git tag -a <tagname> <commit> 特定コミットにタグを作成
git tag -d <tagname> タグを削除
git show <tagname> タグの詳細を表示
git push origin <tagname> タグをリモートにプッシュ
git push origin --tags すべてのタグをプッシュ
git push origin --delete <tagname> リモートのタグを削除
1
2
3
4
5
6
7
8
# 注釈付きタグを作成
git tag -a v1.0.0 -m "Release version 1.0.0"

# タグ一覧を表示
git tag -l "v1.*"

# タグをプッシュ
git push origin v1.0.0

応用コマンド

チェリーピック

コマンド 説明
git cherry-pick <commit> 指定コミットを現在のブランチに適用
git cherry-pick <commit1> <commit2> 複数コミットを順番に適用
git cherry-pick <commit1>..<commit2> 範囲指定でコミットを適用
git cherry-pick -n <commit> コミットせずに変更のみ適用
git cherry-pick --continue コンフリクト解消後に続行
git cherry-pick --abort cherry-pickを中止
1
2
3
4
5
6
7
8
# 特定コミットを適用
git cherry-pick abc1234

# 複数コミットを適用
git cherry-pick abc1234 def5678

# コミットせずに変更のみ適用
git cherry-pick -n abc1234

バイセクト(バグ探索)

コマンド 説明
git bisect start バイセクトを開始
git bisect bad 現在のコミットをバグありとマーク
git bisect good <commit> 指定コミットをバグなしとマーク
git bisect reset バイセクトを終了して元の状態に戻る
git bisect run <script> スクリプトで自動的にバグを探索
git bisect log バイセクトの履歴を表示
git bisect skip 現在のコミットをスキップ
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# バイセクトを開始
git bisect start

# 現在のコミットはバグあり
git bisect bad

# このコミットはバグなし
git bisect good v1.0.0

# 自動テストで探索
git bisect run npm test

# 終了
git bisect reset

ワークツリー(複数作業ディレクトリ)

コマンド 説明
git worktree add <path> <branch> 新しいワークツリーを追加
git worktree add -b <branch> <path> 新しいブランチを作成してワークツリーを追加
git worktree list ワークツリー一覧を表示
git worktree remove <path> ワークツリーを削除
git worktree prune 無効なワークツリー情報を削除
1
2
3
4
5
6
7
8
# 別ブランチ用のワークツリーを追加
git worktree add ../hotfix hotfix/urgent-fix

# ワークツリー一覧
git worktree list

# ワークツリーを削除
git worktree remove ../hotfix

サブモジュールとサブツリー

コマンド 説明
git submodule add <url> <path> サブモジュールを追加
git submodule init サブモジュールを初期化
git submodule update サブモジュールを更新
git submodule update --init --recursive すべてのサブモジュールを再帰的に初期化・更新
git submodule foreach <command> 各サブモジュールでコマンドを実行
git subtree add --prefix=<dir> <url> <branch> サブツリーを追加
git subtree pull --prefix=<dir> <url> <branch> サブツリーを更新
git subtree push --prefix=<dir> <url> <branch> サブツリーの変更をプッシュ
1
2
3
4
5
6
7
8
# サブモジュールを追加
git submodule add https://github.com/user/lib.git libs/lib

# サブモジュールを再帰的に初期化・更新
git submodule update --init --recursive

# サブツリーを追加
git subtree add --prefix=vendor/lib https://github.com/user/lib.git main --squash

設定とカスタマイズコマンド

Git設定

コマンド 説明
git config --list すべての設定を表示
git config --global user.name "<name>" ユーザー名を設定
git config --global user.email "<email>" メールアドレスを設定
git config --global core.editor "<editor>" エディタを設定
git config --global init.defaultBranch main デフォルトブランチ名を設定
git config --global pull.rebase true pull時にリベースを使用
git config --global push.default current 現在のブランチをプッシュ
git config --global alias.<name> "<command>" エイリアスを設定
git config --global --unset <key> 設定を削除
1
2
3
4
5
6
7
8
9
# ユーザー情報を設定
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# エディタを設定
git config --global core.editor "code --wait"

# デフォルトブランチをmainに設定
git config --global init.defaultBranch main

便利なエイリアス設定例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# ステータス短縮表示
git config --global alias.st "status -s"

# グラフ付きログ
git config --global alias.lg "log --oneline --graph --all"

# 直前のコミットを修正
git config --global alias.amend "commit --amend --no-edit"

# ブランチ一覧(最終コミット順)
git config --global alias.br "branch --sort=-committerdate"

# 差分確認
git config --global alias.df "diff --stat"

# コミット取り消し
git config --global alias.undo "reset --soft HEAD~1"

# stash一覧
git config --global alias.sl "stash list"

履歴復元コマンド

reflog操作

コマンド 説明
git reflog HEADの移動履歴を表示
git reflog show <branch> 特定ブランチのreflogを表示
git reflog expire --expire=90.days.ago --all 古いreflogエントリを削除
git reset --hard HEAD@{n} reflogの特定時点に復元
1
2
3
4
5
6
7
8
# reflogを確認
git reflog

# 特定時点に復元
git reset --hard HEAD@{3}

# 削除されたブランチを復元
git branch recovered-branch HEAD@{5}

ファイル操作コマンド

ファイルの移動と削除

コマンド 説明
git mv <old> <new> ファイルを移動/リネーム
git rm <file> ファイルを削除(ステージング含む)
git rm --cached <file> 追跡を解除(ファイルは保持)
git rm -r --cached <dir> ディレクトリの追跡を再帰的に解除
1
2
3
4
5
# ファイルをリネーム
git mv old-name.js new-name.js

# 追跡を解除(ファイルは残す)
git rm --cached config/secrets.json

特定ファイルの検索

コマンド 説明
git ls-files 追跡中のファイル一覧を表示
git ls-files --others 未追跡ファイル一覧を表示
git ls-tree -r HEAD HEADのファイルツリーを表示
git grep "<pattern>" 追跡ファイル内を検索
git grep -n "<pattern>" 行番号付きで検索
1
2
3
4
5
# 追跡ファイル内を検索
git grep -n "TODO"

# 特定ディレクトリ内を検索
git grep "function" -- "*.js"

トラブルシューティング用コマンド

問題診断

コマンド 説明
git fsck リポジトリの整合性をチェック
git gc 不要なオブジェクトを削除して最適化
git prune 到達不能なオブジェクトを削除
git count-objects -v オブジェクトデータベースの統計を表示
git verify-pack -v <pack> packファイルの内容を検証
1
2
3
4
5
6
7
8
# リポジトリの整合性チェック
git fsck

# ガベージコレクション
git gc --prune=now

# オブジェクト統計
git count-objects -v

大容量ファイルの特定

1
2
3
4
# サイズの大きいオブジェクトを特定
git rev-list --objects --all | \
  git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | \
  sort -k3 -n -r | head -20

コマンドオプション早見表

よく使う共通オプション

オプション 説明 対応コマンド
-n / --dry-run 実行せずに結果を表示 add, clean, push
-v / --verbose 詳細な出力を表示 commit, push, fetch
-q / --quiet 出力を抑制 add, commit, checkout
-f / --force 強制実行 branch, push, checkout
-p / --patch 対話的な部分選択 add, checkout, stash

よく使うコマンドの組み合わせ

日常のワークフロー

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 作業開始
git switch -c feature/new-feature
git pull --rebase origin main

# 開発中
git add -p
git commit -m "feat: 機能を追加"

# プッシュ前の確認
git log --oneline origin/main..HEAD
git push -u origin feature/new-feature

コンフリクト解消

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# マージでコンフリクト発生時
git status  # コンフリクトファイルを確認
# ファイルを編集してコンフリクトを解消
git add <file>
git commit  # マージコミット

# リベースでコンフリクト発生時
git status  # コンフリクトファイルを確認
# ファイルを編集してコンフリクトを解消
git add <file>
git rebase --continue

誤操作からの復旧

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 直前のコミットを取り消し(変更は保持)
git reset --soft HEAD~1

# 強制プッシュ後の復旧
git reflog
git reset --hard HEAD@{1}
git push --force-with-lease

# 削除したブランチの復元
git reflog
git branch recovered-branch <commit>

まとめ

このチートシートでは、Gitの基本操作から応用テクニックまで、実務でよく使用するコマンドを網羅的に紹介しました。すべてのコマンドを暗記する必要はありません。必要なときにこのチートシートを参照し、徐々に使用頻度の高いコマンドを覚えていくことをおすすめします。

Gitのコマンドは非常に多機能で、オプションの組み合わせによってさまざまな操作が可能です。まずは基本操作(init、add、commit、branch、merge、push、pull)を確実に使いこなし、必要に応じて応用コマンドを学んでいきましょう。

参考リンク