エンジニアに必須のGit・GitHub。基本コマンドからブランチ戦略、GitHub ActionsによるCI/CD、PRのベストプラクティスまで実践的に完全解説します。
Gitの基本コマンドリファレンス
# リポジトリ初期設定
git init
git clone URL
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
# 基本操作
git status
git add .
git commit -m "メッセージ"
git push origin main
git pull origin main
# ブランチ操作
git branch feature/new-feature
git checkout -b feature/new-feature
git merge feature/new-feature
git branch -d feature/new-feature
# 便利コマンド
git log --oneline --graph
git stash / git stash pop
git reset --soft HEAD~1
git rebase -i HEAD~3
GitHub Actionsで自動化(CI/CD)
name: CI/CD Pipeline
on:
push:
branches: [ main ]
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
ブランチ戦略の選び方
- GitHub Flow:小〜中規模チーム向け。mainブランチから直接feature branchを作成してPRでマージ。シンプルで高速なデプロイに最適
- Git Flow:大規模プロジェクト向け。main/develop/feature/release/hotfixの5種類のブランチで管理
- Trunk Based Development:Google・Facebook等が採用。短命なfeature branchと頻繁なintegrationが特徴
プルリクエスト(PR)のベストプラクティス
- PRは小さく保つ(1PRあたり200〜400行が理想)
- タイトルに種別を明記:feat/fix/docs/chore/refactor
- 説明文に「何を・なぜ変更したか」を記載
- UI変更はスクリーンショットを添付
- Branch Protection Rulesでレビューを必須にする
よくあるGitトラブルと解決法
| トラブル | 解決コマンド |
|---|---|
| コンフリクト発生 | git mergetool |
| コミットメッセージ修正 | git commit –amend |
| push済みコミット取消 | git revert HEAD |
| 機密ファイルをコミットした | git rm –cached ファイル名 |
Gitをマスターすることで、チーム開発の生産性が劇的に向上します。まず基本コマンドを覚え、GitHub Actionsで自動化を取り入れることをおすすめします。