Git完全チートシート - 実務で使う全50コマンド一覧
実務で本当に使うGitコマンドを完全網羅。このチートシートがあれば、もう困りません。
基本コマンド
ヘルプ・情報確認
# ヘルプ表示
git help <command>
git <command> --help
# バージョン確認
git version
# 設定確認
git config --list
git config user.name
git config --show-origin user.name
初期設定
# ユーザー情報設定(必須)
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# エディタ設定
git config --global core.editor vim
# 改行コード設定(Windows)
git config --global core.autocrlf true
# 改行コード設定(Mac/Linux)
git config --global core.autocrlf input
# プル戦略設定
git config --global pull.rebase false # merge戦略
git config --global pull.rebase true # rebase戦略
リポジトリ操作
新規作成・クローン
# 新規リポジトリ作成
git init
# リモートからクローン
git clone <url>
git clone <url> <directory-name>
# 特定ブランチをクローン
git clone -b <branch> <url>
日常の作業
変更の記録
# 状態確認
git status
git status -s # 短縮表示
# ファイルをステージング
git add <file>
git add . # すべて
git add *.js # 拡張子指定
git add src/ # ディレクトリ指定
# インタラクティブステージング
git add -p # 対話的に選択
git add -i # 対話モード
# コミット
git commit -m "message"
git commit -am "message" # add + commit
git commit --amend # 直前のコミット修正
git commit --amend --no-edit # メッセージそのまま
変更内容の確認
# 差分確認
git diff # Working Tree vs Staging
git diff --staged # Staging vs Repository
git diff --cached # 同上
git diff HEAD # Working Tree vs Repository
git diff <branch1> <branch2>
# コミット履歴確認
git log
git log --oneline # 1行表示
git log --graph # グラフ表示
git log --all --graph # すべてのブランチ
git log -p # 差分付き
git log -n 5 # 最新5件
git log --since="2 weeks ago"
git log --author="Name"
git log --grep="keyword"
git log -- <file> # ファイルの履歴
# コミット詳細表示
git show <commit>
git show <commit>:<file> # 特定ファイルの内容
git show HEAD # 最新コミット
git show HEAD~1 # 1つ前のコミット
ファイル操作
# ファイル削除
git rm <file>
git rm --cached <file> # Gitから削除、ファイルは残す
git rm -r <directory> # ディレクトリ削除
# ファイル移動・リネーム
git mv <old> <new>
# 未追跡ファイル削除(危険)
git clean -n # 確認(dry-run)
git clean -f # ファイル削除
git clean -fd # ファイル+ディレクトリ削除
git clean -fdx # .gitignoreも削除
変更の取り消し
# ファイルを最後のコミット状態に戻す
git restore <file>
git restore . # すべて
# アンステージング
git restore --staged <file>
git restore --staged .
# コミットの打ち消し(安全)
git revert <commit>
git revert HEAD # 最新コミットを打ち消し
git revert --no-commit <commit> # コミットせず
# 歴史の巻き戻し(危険)
git reset --soft HEAD~1 # コミットのみ取り消し
git reset --mixed HEAD~1 # ステージングも取り消し(デフォルト)
git reset --hard HEAD~1 # すべて取り消し(危険)
ブランチ操作
ブランチの作成・切り替え
# ブランチ一覧
git branch
git branch -a # リモートも含む
git branch -v # コミットも表示
# ブランチ作成
git branch <branch-name>
# ブランチ切り替え
git checkout <branch>
git switch <branch> # 新しい方法
# ブランチ作成+切り替え
git checkout -b <branch>
git switch -c <branch> # 新しい方法
# ブランチ削除
git branch -d <branch> # マージ済みのみ
git branch -D <branch> # 強制削除
マージ
# マージ
git merge <branch>
git merge --no-ff <branch> # Fast-forwardしない
git merge --squash <branch> # squashマージ
# マージ中断
git merge --abort
# コンフリクト確認
git status
git diff
リベース
# リベース
git rebase <branch>
git rebase -i <commit> # インタラクティブ
git rebase --continue # 続行
git rebase --abort # 中断
git rebase --skip # スキップ
Cherry-pick
# 特定のコミットを適用
git cherry-pick <commit>
git cherry-pick <commit1> <commit2>
git cherry-pick --continue
git cherry-pick --abort
リモート操作
リモート設定
# リモート一覧
git remote
git remote -v # URL付き
# リモート追加
git remote add <name> <url>
git remote add origin <url>
# リモート削除
git remote remove <name>
# リモート名変更
git remote rename <old> <new>
# リモート情報表示
git remote show <name>
フェッチ・プル・プッシュ
# リモートから取得(マージしない)
git fetch
git fetch <remote>
git fetch --all
# 取得+マージ
git pull
git pull <remote> <branch>
git pull --rebase # rebase戦略
# プッシュ
git push
git push <remote> <branch>
git push -u origin main # 初回、上流設定
git push --force # 強制(危険)
git push --force-with-lease # より安全な強制
git push --all # すべてのブランチ
git push --tags # タグもプッシュ
タグ
# タグ一覧
git tag
git tag -l "v1.*" # パターン検索
# 軽量タグ作成
git tag <tag-name>
# 注釈付きタグ作成
git tag -a <tag-name> -m "message"
# タグをリモートにプッシュ
git push origin <tag-name>
git push --tags # すべてのタグ
# タグ削除
git tag -d <tag-name> # ローカル
git push origin :refs/tags/<tag-name> # リモート
一時保存
# スタッシュ
git stash # 保存
git stash save "message" # メッセージ付き
git stash -u # 未追跡ファイルも含む
# スタッシュ一覧
git stash list
# スタッシュ適用
git stash pop # 適用+削除
git stash apply # 適用のみ
git stash apply stash@{0} # 特定のスタッシュ
# スタッシュ削除
git stash drop stash@{0}
git stash clear # すべて削除
# スタッシュ内容確認
git stash show
git stash show -p # 差分表示
検索・調査
履歴検索
# コード検索
git grep "keyword"
git grep -n "keyword" # 行番号付き
git grep --count "keyword" # ファイルごとの出現数
# 誰がいつ変更したか
git blame <file>
git blame -L 10,20 <file> # 特定行
git annotate <file> # blameの別名
# バグの原因特定(二分探索)
git bisect start
git bisect bad # 現在はバグあり
git bisect good <commit> # このコミットはOK
git bisect reset # 終了
# 参照ログ(履歴の履歴)
git reflog
git reflog show HEAD
git reflog show <branch>
高度なツール
Worktree(複数作業ツリー)
# 作業ツリー追加
git worktree add <path> <branch>
git worktree add ../hotfix hotfix
# 作業ツリー一覧
git worktree list
# 作業ツリー削除
git worktree remove <path>
git worktree prune # クリーンアップ
バージョン記述
# タグベースのバージョン番号生成
git describe
git describe --tags
git describe --always # タグがなくてもハッシュ表示
コミット要約
# コントリビューター一覧
git shortlog
git shortlog -sn # 件数でソート
git shortlog --no-merges # マージコミット除外
git shortlog v1.0.0..v1.5.0 # 範囲指定
リリース管理
アーカイブ作成
# リリース用アーカイブ
git archive main --format=zip > release.zip
git archive main --format=tar.gz > release.tar.gz
git archive main --prefix='project/' > release.tar
git archive v1.5.0 --format=zip > v1.5.0.zip
バンドル(オフライン転送)
# バンドル作成
git bundle create repo.bundle HEAD main
git bundle create repo.bundle --all
# バンドル検証
git bundle verify repo.bundle
# バンドルからクローン
git clone repo.bundle project
# バンドルからフェッチ
git fetch ../repo.bundle main:main
パッチワークフロー
# パッチ生成
git format-patch -1 HEAD # 最新1件
git format-patch -3 HEAD # 最新3件
git format-patch main..feature # 範囲指定
# パッチ適用(ファイル変更のみ)
git apply patch-file.patch
# パッチ適用(コミットまで)
git am patch-file.patch
git am --continue
git am --abort
# プル要求メッセージ生成
git request-pull main origin feature
# メール送信
git send-email *.patch
複数リポジトリ管理
Submodule
# サブモジュール追加
git submodule add <url> <path>
# サブモジュール初期化
git submodule init
git submodule update
# まとめて実行
git submodule update --init --recursive
# サブモジュール削除
git submodule deinit <path>
git rm <path>
# サブモジュール更新
git submodule update --remote
Subtree
# サブツリー追加
git subtree add --prefix=<path> <url> <branch> --squash
# サブツリー更新
git subtree pull --prefix=<path> <url> <branch> --squash
# サブツリーにプッシュ
git subtree push --prefix=<path> <url> <branch>
メンテナンス
# ガベージコレクション
git gc # 通常
git gc --aggressive # 完全圧縮(遅い)
git gc --auto # 自動判定
# リポジトリ検証
git fsck # 整合性チェック
git fsck --full # 完全チェック
# リポジトリサイズ確認
git count-objects -vH
その他の便利コマンド
設定・カスタマイズ
# エイリアス設定
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'
# マージ戦略設定
git config --global merge.conflictstyle diff3
git config --global rerere.enabled true
コミットメモ
# コミットにメモ追加(履歴を変えない)
git notes add -m "追加情報"
git notes show
git notes edit
git notes remove
大規模リポジトリ
# 部分チェックアウト設定
git sparse-checkout init --cone
git sparse-checkout set <directory>
git sparse-checkout add <directory>
git sparse-checkout list
git sparse-checkout disable
履歴の比較
# リベース前後の比較
git range-diff <base> <old-branch> <new-branch>
git range-diff main topic@{1} topic
危険なコマンド(要注意)
# 履歴の大規模書き換え
git filter-repo --path <path> # 推奨
git filter-branch --tree-filter <command> # 非推奨
# オブジェクト置換(実験的)
git replace <old-commit> <new-commit>
git replace --delete <commit>
# 強制プッシュ
git push --force # 危険
git push --force-with-lease # より安全
よく使う組み合わせ
緊急ホットフィックス
git stash # 作業を退避
git checkout main # mainに移動
git pull # 最新を取得
git checkout -b hotfix # hotfixブランチ作成
# 修正作業
git add .
git commit -m "Fix: ..."
git push -u origin hotfix
# プルリクエスト作成
git checkout - # 元のブランチに戻る
git stash pop # 作業を復元
コミットメッセージ間違えた
git commit --amend -m "正しいメッセージ"
間違ったファイルをコミットした
git reset --soft HEAD~1 # コミット取り消し
git restore --staged <file> # 特定ファイルをアンステージ
git commit # 再コミット
他人のブランチで作業してしまった
git stash # 作業を退避
git checkout main # mainに移動
git checkout -b my-feature # 自分のブランチ作成
git stash pop # 作業を復元
トラブルシューティング
# 「detached HEAD」状態から脱出
git checkout -b new-branch # 新ブランチ作成
# または
git checkout main # mainに戻る
# マージを取り消す
git merge --abort
git reset --hard HEAD~1 # マージコミットを削除
# プッシュを取り消す
git revert HEAD
git push
# コンフリクトを解決
git status # 競合ファイル確認
# ファイルを編集して解決
git add .
git commit
# 間違えてファイルを削除した
git restore <file>
git checkout HEAD -- <file>
# 消したコミットを復元
git reflog # コミットハッシュ確認
git cherry-pick <hash> # コミット復元
