コンテンツにスキップ

基本的なGit操作

この章では、Git の基本操作であるコミット、プッシュ、プルを学びます。これらは日常的に最も使う操作です。

初めてGitを使う場合、ユーザー情報を設定します。

Terminal window
# 必須設定
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

Gitには3つの状態があります。

┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 作業ディレクトリ │ │ ステージングエリア │ │ リポジトリ │
│ (Working Dir) │────▶│ (Staging Area) │────▶│ (Repository) │
│ │ add │ │commit│ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
↑ │
└────────────────────────────────────────────────┘
checkout / reset
状態説明
作業ディレクトリ実際にファイルを編集する場所
ステージングエリア次のコミットに含める変更を置く場所
リポジトリコミットされた変更履歴

なぜステージングエリアがあるのか

Section titled “なぜステージングエリアがあるのか”
Terminal window
# 例:3つのファイルを変更したが、2つだけコミットしたい
git add file1.js file2.js
git commit -m "機能Aを実装"
# 残りは別のコミットに
git add file3.js
git commit -m "機能Bを実装"
Terminal window
# 1. 変更状態を確認
git status
# 2. 変更をステージングに追加
git add <ファイル名>
# 3. コミット
git commit -m "コミットメッセージ"
Terminal window
# 特定のファイルを追加
git add file1.js file2.js
# 特定のディレクトリを追加
git add src/
# すべての変更を追加
git add .
# すべての変更を追加(削除も含む)
git add -A
# 対話的に追加(部分的な変更をステージング)
git add -p
Terminal window
# 基本
git commit -m "コミットメッセージ"
# 複数行のメッセージ
git commit -m "タイトル" -m "詳細な説明"
# エディタでメッセージを書く
git commit
# add と commit を同時に(追跡済みファイルのみ)
git commit -am "メッセージ"
# 直前のコミットを修正
git commit --amend
<種類>: <要約>(50文字以内)
<本文>(任意、72文字で折り返し)
<フッター>(任意)

例:

feat: ユーザー認証機能を追加
- ログイン/ログアウト機能を実装
- セッション管理を追加
- パスワードハッシュ化を実装
Closes #123
種類のプレフィックス(Conventional Commits) - `feat`: 新機能 - `fix`: バグ修正 - `docs`: ドキュメントのみ - `style`: フォーマット変更(動作に影響なし) - `refactor`: リファクタリング - `test`: テスト追加・修正 - `chore`: ビルドプロセスや補助ツールの変更

ローカルのコミットをリモートに送信します。

Terminal window
# 基本
git push origin main
# 初回(上流ブランチを設定)
git push -u origin main
# 上流ブランチ設定後
git push
# 強制プッシュ(⚠️注意が必要)
git push --force-with-lease

リモートの変更をローカルに取り込みます。

Terminal window
# 基本
git pull origin main
# 上流ブランチ設定後
git pull
# リベースでプル
git pull --rebase
ローカル リモート(GitHub)
┌─────────┐ ┌─────────┐
│ commit │ │ commit │
│ commit │─────── push ─────────▶│ commit │
│ commit │ │ commit │
│ │◀────── pull ──────────│ (新commit)│
└─────────┘ └─────────┘

リモートの情報を取得するだけ(マージしない):

Terminal window
# リモートの最新情報を取得
git fetch origin
# 全てのリモートを取得
git fetch --all
# 削除されたブランチの参照を削除
git fetch --prune
pull = fetch + merge
Terminal window
git status
# 短縮形式
git status -s
# M modified.js # 変更あり(ステージング済み)
# A new-file.js # 新規追加
# D deleted.js # 削除
# ?? untracked.js # 追跡されていない
Terminal window
# 作業ディレクトリの変更(ステージング前)
git diff
# ステージングエリアの変更
git diff --staged
# 特定のコミット間の差分
git diff commit1 commit2
# ファイル名のみ表示
git diff --name-only
Terminal window
# 基本
git log
# 1行表示
git log --oneline
# グラフ表示
git log --oneline --graph
# 直近n件
git log -n 5
# 特定ファイルの履歴
git log -- path/to/file
# 検索
git log --grep="キーワード"
Terminal window
# 特定のファイルをステージングから除外
git restore --staged file.js
# すべてをステージングから除外
git restore --staged .
# 旧コマンド
git reset HEAD file.js

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

Section titled “作業ディレクトリの変更を取り消し”
Terminal window
# 特定のファイルの変更を破棄
git restore file.js
# すべての変更を破棄
git restore .
# 旧コマンド
git checkout -- file.js
Terminal window
# 直前のコミットを取り消し(変更は残す)
git reset --soft HEAD~1
# 直前のコミットを取り消し(ステージングも解除)
git reset HEAD~1
# 直前のコミットを完全に取り消し(変更も破棄)⚠️
git reset --hard HEAD~1
# 取り消しをコミットとして記録(安全)
git revert HEAD
コマンド変更ステージングコミット
reset --soft残る残る消える
reset (--mixed)残る消える消える
reset --hard消える消える消える
revert新コミット-履歴に残る

作業中の変更を一時的に退避できます。

Terminal window
# 変更を退避
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

インタラクティブなステージング

Section titled “インタラクティブなステージング”
Terminal window
git add -p
# 選択肢
# y - このハンクをステージング
# n - スキップ
# s - ハンクを分割
# e - 手動で編集
Terminal window
# 直近3つのコミットを整理
git rebase -i HEAD~3
# エディタで操作を選択
# pick - そのまま
# reword - メッセージ変更
# squash - 前のコミットと統合
# drop - 削除

reset --hard で消したコミットも復元可能:

Terminal window
git reflog
# abc1234 HEAD@{0}: reset: moving to HEAD~1
# def5678 HEAD@{1}: commit: 重要なコミット
# 復元
git reset --hard def5678
Terminal window
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m"
git config --global alias.lg "log --oneline --graph --all"
# 使用例
git st
git lg

操作コマンド説明
状態確認git status変更状態を確認
差分確認git diff変更内容を確認
ステージングgit addコミット対象に追加
コミットgit commit変更を記録
プッシュgit pushリモートに送信
プルgit pullリモートから取得
取り消しgit restore変更を破棄
退避git stash一時保存

次の章では、ブランチ管理について学びます。

Q1. Gitの3つの状態について正しい順序はどれですか?

Q2. git pullコマンドは何と何を組み合わせたものですか?

Q3. コミットメッセージのプレフィックスで、新機能追加を表すものはどれですか?