Gitとは?バージョン管理の基礎を理解する
GitはLinusトーバルズが開発した分散型バージョン管理システムです。ソースコードの変更履歴を管理し、複数人での開発を効率化します。2026年現在、Gitは世界中のエンジニアが使う事実上の標準ツールとなっています。
Gitのインストールと初期設定
# インストール確認
git --version
# ユーザー情報の設定(必須)
git config --global user.name 'Your Name'
git config --global user.email 'your@email.com'
# デフォルトエディタの設定
git config --global core.editor 'code --wait' # VSCode
# 設定の確認
git config --list
# 改行コードの設定(macOS/Linux)
git config --global core.autocrlf input
# 改行コードの設定(Windows)
git config --global core.autocrlf true
基本的なGitコマンド
# リポジトリの初期化
mkdir my-project && cd my-project
git init
# ファイルの追跡(ステージング)
git add file.txt # 特定ファイル
git add . # 全ての変更
git add -p # インタラクティブに選択
# コミット
git commit -m 'feat: 新機能を追加'
git commit --amend # 直前のコミットを修正
# 状態確認
git status
git log --oneline --graph --all
git diff # 変更の確認
git diff HEAD~1 HEAD # 特定コミット間の差分
# リモートリポジトリ
git remote add origin https://github.com/user/repo.git
git push -u origin main
git pull origin main
git fetch --all # リモートの情報を取得(マージなし)
ブランチ操作
# ブランチの作成・切り替え
git branch feature/new-feature
git checkout feature/new-feature
# または(Git 2.23以降の推奨方法)
git switch -c feature/new-feature
# ブランチ一覧
git branch -a # リモートブランチも表示
# マージ
git switch main
git merge feature/new-feature
git merge --no-ff feature/new-feature # マージコミットを強制
# リベース
git switch feature/new-feature
git rebase main
# ブランチの削除
git branch -d feature/new-feature # マージ済みのみ
git branch -D feature/new-feature # 強制削除
コンフリクトの解決
# コンフリクトが発生した場合
git merge feature/branch # コンフリクト発生
# コンフリクトファイルを手動で編集
# <<<<<<< HEAD
# 現在のブランチの内容
# =======
# マージするブランチの内容
# >>>>>>> feature/branch
# 解決後
git add conflicted-file.txt
git commit -m 'fix: コンフリクトを解決'
# マージを中断する場合
git merge --abort
便利なGitコマンド集
# stash: 作業を一時退避
git stash save 'WIP: 機能実装中'
git stash list
git stash pop # 最新のstashを復元
git stash apply stash@{1} # 特定のstashを復元
git stash drop # stashを削除
# cherry-pick: 特定コミットを取り込む
git cherry-pick abc1234
# reset: コミットの取り消し
git reset --soft HEAD~1 # コミットのみ取り消し(変更は残る)
git reset --mixed HEAD~1 # コミット+ステージング取り消し
git reset --hard HEAD~1 # 全て取り消し(危険:変更も失う)
# revert: コミットを安全に取り消す
git revert HEAD # 最新コミットを打ち消すコミットを作成
# bisect: バグが混入したコミットを二分探索で特定
git bisect start
git bisect bad # 現在はバグあり
git bisect good v1.0 # v1.0はバグなし
# Gitが中間コミットをcheckoutするので、バグの有無を判定
git bisect good # or git bisect bad
git bisect reset # 終了
GitHubでのプルリクエスト(PR)運用
効果的なブランチ戦略
- GitHub Flow:mainブランチ + feature branchのシンプルな戦略。小規模チーム向け
- Git Flow:main, develop, feature, release, hotfixの複雑なブランチ戦略。大規模開発向け
- Trunk-Based Development:短命のブランチで頻繁にmainにマージ。CI/CDと相性良い
PRの書き方のベストプラクティス
## 変更内容
- ユーザー認証のJWT実装を追加
- リフレッシュトークンの自動更新機能
## 動機・背景
セッション管理をよりセキュアにするため...
## テスト方法
1. npm test で全テストがパスすることを確認
2. POST /api/auth/login でトークン取得
3. Authorization: Bearer xxx でAPIアクセス
## 影響範囲
- /api/auth/* エンドポイント
- 既存のセッション認証との後方互換性あり
## スクリーンショット(UI変更がある場合)
.gitignoreの設定
# .gitignore の例(Node.js プロジェクト)
node_modules/
.env
.env.local
.env.*.local
dist/
build/
*.log
.DS_Store
.vscode/
!.vscode/extensions.json
*.swp
Gitのエイリアス設定(作業効率化)
# よく使うコマンドをエイリアスに登録
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg 'log --oneline --graph --all --decorate'
git config --global alias.last 'log -1 HEAD --stat'
git config --global alias.unstage 'reset HEAD --'
まとめ:Gitをマスターしてチーム開発で活躍しよう
- ✅ add・commit・push・pullの基本フローを身につける
- ✅ ブランチを活用して並行作業を効率化
- ✅ stash・cherry-pick・rebaseを使いこなす
- ✅ コンフリクトを恐れずに解決できるようになる
- ✅ PRのレビュー文化を取り入れてコード品質を向上