GitとGitHubはすべてのエンジニアにとって必須のツールです。2026年現在、Gitを知らないエンジニアはほぼ存在しないと言えるほど普及しています。本記事では、Git/GitHubの基礎から実践的なブランチ戦略まで徹底解説します。
Gitとは?バージョン管理の基礎
Gitは分散型バージョン管理システムです。コードの変更履歴を管理し、複数人での開発を効率化します。Linus Torvalds(Linuxの作者)が開発しました。
必須Gitコマンド一覧
# 初期設定
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# リポジトリ初期化・クローン
git init
git clone https://github.com/username/repo.git
# 基本操作
git status # 変更状態確認
git add . # 全ファイルをステージング
git add src/file.ts # 特定ファイルをステージング
git commit -m "feat: add login function" # コミット
# ブランチ操作
git branch # ブランチ一覧
git checkout -b feature/login # 新ブランチ作成&移動
git switch main # mainブランチに移動(推奨コマンド)
git merge feature/login # マージ
# リモート操作
git push origin feature/login # リモートにプッシュ
git pull origin main # リモートから取得
git fetch --all # 全リモートブランチ取得
実践的なブランチ戦略
GitHub Flow(シンプルで人気)
# GitHub Flowの基本フロー
1. mainブランチから feature/xxx ブランチを作成
2. 機能開発・コミット
3. GitHubにプッシュ
4. Pull Requestを作成
5. コードレビュー
6. mainにマージ
7. デプロイ
# 例
git switch main
git pull origin main
git switch -c feature/user-auth
# 開発作業...
git add .
git commit -m "feat: implement JWT authentication"
git push origin feature/user-auth
# GitHub上でPull Request作成 →レビュー→マージ
Git Flow(大規模プロジェクト向け)
# Git Flowのブランチ構造
main # 本番リリース用
develop # 開発統合ブランチ
feature/* # 機能開発
release/* # リリース準備
hotfix/* # 緊急バグ修正
# git-flowツールの使用
brew install git-flow
git flow init
git flow feature start user-auth
git flow feature finish user-auth
コミットメッセージのベストプラクティス
# Conventional Commits(業界標準)
feat: 新機能追加
fix: バグ修正
docs: ドキュメント変更
style: コードスタイル(動作影響なし)
refactor: リファクタリング
test: テスト追加・修正
chore: ビルド・補助ツール変更
# 例
feat(auth): add OAuth2 Google login
fix(api): resolve null pointer in user endpoint
docs: update README with setup instructions
GitHub ActionsでCI/CDを構築
# .github/workflows/ci.yml
name: CI/CD Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm test
- run: npm run build
deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Deploy to production
run: |
echo "Deploying to production..."
まとめ
Git/GitHubはエンジニアの基本ツールです。まずは基本コマンドをマスターし、GitHub Flowでチーム開発を始めましょう。GitHub Actionsを使えばCI/CDの自動化も簡単に実現できます。