基本的なGit操作
この章では、Git の基本操作であるコミット、プッシュ、プルを学びます。これらは日常的に最も使う操作です。
Gitの初期設定
Section titled “Gitの初期設定”初めてGitを使う場合、ユーザー情報を設定します。
# 必須設定git config --global user.name "Your Name"git config --global user.email "your_email@example.com"
# 推奨設定git config --global init.defaultBranch main # デフォルトブランチ名git config --global core.editor "code --wait" # エディタ(VS Code)git config --global pull.rebase false # pullの挙動
# 設定確認git config --listステージングエリアの理解
Section titled “ステージングエリアの理解”Gitには3つの状態があります。
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐│ 作業ディレクトリ │ │ ステージングエリア │ │ リポジトリ ││ (Working Dir) │────▶│ (Staging Area) │────▶│ (Repository) ││ │ add │ │commit│ │└─────────────────┘ └─────────────────┘ └─────────────────┘ ↑ │ └────────────────────────────────────────────────┘ checkout / reset| 状態 | 説明 |
|---|---|
| 作業ディレクトリ | 実際にファイルを編集する場所 |
| ステージングエリア | 次のコミットに含める変更を置く場所 |
| リポジトリ | コミットされた変更履歴 |
なぜステージングエリアがあるのか
Section titled “なぜステージングエリアがあるのか”# 例:3つのファイルを変更したが、2つだけコミットしたいgit add file1.js file2.jsgit commit -m "機能Aを実装"
# 残りは別のコミットにgit add file3.jsgit commit -m "機能Bを実装"コミットの作成
Section titled “コミットの作成”基本的な流れ
Section titled “基本的な流れ”# 1. 変更状態を確認git status
# 2. 変更をステージングに追加git add <ファイル名>
# 3. コミットgit commit -m "コミットメッセージ"addのオプション
Section titled “addのオプション”# 特定のファイルを追加git add file1.js file2.js
# 特定のディレクトリを追加git add src/
# すべての変更を追加git add .
# すべての変更を追加(削除も含む)git add -A
# 対話的に追加(部分的な変更をステージング)git add -pcommitのオプション
Section titled “commitのオプション”# 基本git commit -m "コミットメッセージ"
# 複数行のメッセージgit commit -m "タイトル" -m "詳細な説明"
# エディタでメッセージを書くgit commit
# add と commit を同時に(追跡済みファイルのみ)git commit -am "メッセージ"
# 直前のコミットを修正git commit --amendコミットメッセージの書き方
Section titled “コミットメッセージの書き方”<種類>: <要約>(50文字以内)
<本文>(任意、72文字で折り返し)
<フッター>(任意)例:
feat: ユーザー認証機能を追加
- ログイン/ログアウト機能を実装- セッション管理を追加- パスワードハッシュ化を実装
Closes #123種類のプレフィックス(Conventional Commits)
- `feat`: 新機能 - `fix`: バグ修正 - `docs`: ドキュメントのみ - `style`: フォーマット変更(動作に影響なし) - `refactor`: リファクタリング - `test`: テスト追加・修正 - `chore`: ビルドプロセスや補助ツールの変更プッシュとプル
Section titled “プッシュとプル”プッシュ(Push)
Section titled “プッシュ(Push)”ローカルのコミットをリモートに送信します。
# 基本git push origin main
# 初回(上流ブランチを設定)git push -u origin main
# 上流ブランチ設定後git push
# 強制プッシュ(⚠️注意が必要)git push --force-with-leaseプル(Pull)
Section titled “プル(Pull)”リモートの変更をローカルに取り込みます。
# 基本git pull origin main
# 上流ブランチ設定後git pull
# リベースでプルgit pull --rebaseプッシュとプルの関係
Section titled “プッシュとプルの関係”ローカル リモート(GitHub)┌─────────┐ ┌─────────┐│ commit │ │ commit ││ commit │─────── push ─────────▶│ commit ││ commit │ │ commit ││ │◀────── pull ──────────│ (新commit)│└─────────┘ └─────────┘フェッチ(Fetch)
Section titled “フェッチ(Fetch)”リモートの情報を取得するだけ(マージしない):
# リモートの最新情報を取得git fetch origin
# 全てのリモートを取得git fetch --all
# 削除されたブランチの参照を削除git fetch --prunepull = fetch + mergestatus - 現在の状態
Section titled “status - 現在の状態”git status
# 短縮形式git status -s# M modified.js # 変更あり(ステージング済み)# A new-file.js # 新規追加# D deleted.js # 削除# ?? untracked.js # 追跡されていないdiff - 差分の確認
Section titled “diff - 差分の確認”# 作業ディレクトリの変更(ステージング前)git diff
# ステージングエリアの変更git diff --staged
# 特定のコミット間の差分git diff commit1 commit2
# ファイル名のみ表示git diff --name-onlylog - 履歴の確認
Section titled “log - 履歴の確認”# 基本git log
# 1行表示git log --oneline
# グラフ表示git log --oneline --graph
# 直近n件git log -n 5
# 特定ファイルの履歴git log -- path/to/file
# 検索git log --grep="キーワード"変更の取り消し
Section titled “変更の取り消し”ステージングの取り消し
Section titled “ステージングの取り消し”# 特定のファイルをステージングから除外git restore --staged file.js
# すべてをステージングから除外git restore --staged .
# 旧コマンドgit reset HEAD file.js作業ディレクトリの変更を取り消し
Section titled “作業ディレクトリの変更を取り消し”# 特定のファイルの変更を破棄git restore file.js
# すべての変更を破棄git restore .
# 旧コマンドgit checkout -- file.jsコミットの取り消し
Section titled “コミットの取り消し”# 直前のコミットを取り消し(変更は残す)git reset --soft HEAD~1
# 直前のコミットを取り消し(ステージングも解除)git reset HEAD~1
# 直前のコミットを完全に取り消し(変更も破棄)⚠️git reset --hard HEAD~1
# 取り消しをコミットとして記録(安全)git revert HEAD| コマンド | 変更 | ステージング | コミット |
|---|---|---|---|
reset --soft | 残る | 残る | 消える |
reset (--mixed) | 残る | 消える | 消える |
reset --hard | 消える | 消える | 消える |
revert | 新コミット | - | 履歴に残る |
一時的な変更の退避(Stash)
Section titled “一時的な変更の退避(Stash)”作業中の変更を一時的に退避できます。
# 変更を退避git stash
# メッセージ付きで退避git stash save "作業中の機能"
# 退避した一覧git stash list
# 最新のstashを復元git stash pop
# 特定のstashを復元git stash apply stash@{1}
# stashの内容を確認git stash show -p stash@{0}
# stashを削除git stash drop stash@{0}
# 全てのstashを削除git stash clear中級者向けTips
Section titled “中級者向けTips”インタラクティブなステージング
Section titled “インタラクティブなステージング”git add -p
# 選択肢# y - このハンクをステージング# n - スキップ# s - ハンクを分割# e - 手動で編集コミットの整理(rebase -i)
Section titled “コミットの整理(rebase -i)”# 直近3つのコミットを整理git rebase -i HEAD~3
# エディタで操作を選択# pick - そのまま# reword - メッセージ変更# squash - 前のコミットと統合# drop - 削除reflog - 全ての操作履歴
Section titled “reflog - 全ての操作履歴”reset --hard で消したコミットも復元可能:
git reflog# abc1234 HEAD@{0}: reset: moving to HEAD~1# def5678 HEAD@{1}: commit: 重要なコミット
# 復元git reset --hard def5678エイリアスの設定
Section titled “エイリアスの設定”git config --global alias.st statusgit config --global alias.co checkoutgit config --global alias.br branchgit config --global alias.cm "commit -m"git config --global alias.lg "log --oneline --graph --all"
# 使用例git stgit lg| 操作 | コマンド | 説明 |
|---|---|---|
| 状態確認 | git status | 変更状態を確認 |
| 差分確認 | git diff | 変更内容を確認 |
| ステージング | git add | コミット対象に追加 |
| コミット | git commit | 変更を記録 |
| プッシュ | git push | リモートに送信 |
| プル | git pull | リモートから取得 |
| 取り消し | git restore | 変更を破棄 |
| 退避 | git stash | 一時保存 |
次の章では、ブランチ管理について学びます。
Q1. Gitの3つの状態について正しい順序はどれですか?
Q2. git pullコマンドは何と何を組み合わせたものですか?
Q3. コミットメッセージのプレフィックスで、新機能追加を表すものはどれですか?