Git入門

コードの変更履歴を記録し、いつでも過去に戻れる分散型バージョン管理システム。

ゲームのセーブポイント (Game Save Point)

Gitは「RPGのセーブ機能」です。 **コミット** = セーブポイント(いつでも戻れる) **ブランチ** = パラレルワールド(本編に影響なく冒険) **マージ** = 世界線の統合(いいとこ取り) ボス戦(本番環境)で全滅しても、セーブポイント(コミット)まで戻ればやり直せます。

用語集

リポジトリ
プロジェクトの保管庫。履歴も含む。
コミット
スナップショット。セーブポイント。
ブランチ
分岐。並行開発の単位。
マージ
ブランチを統合する操作。
リモート
GitHub等のサーバー上のリポジトリ。

なぜGitが必要か

Gitがないと、ファイル名に日付やバージョン番号をつけて管理することになります。

Bad
Terminal window
# Bad: Meaningless commit messages
git commit -m "fix"
git commit -m "update"
git commit -m "asdf"
Good
Terminal window
# Good: Descriptive commit messages (Conventional Commits)
git commit -m "fix: Resolve null pointer in login handler"
git commit -m "feat: Add password validation"
git commit -m "docs: Update README installation steps"
Tip: Conventional Commits 形式(feat:, fix:, docs:)を使うと履歴が読みやすくなる。

基本コマンド

git-basics.sh
# Create a new repository
git init my_project
# Clone an existing repository
git clone https://github.com/user/repo.git
# Check current status
git status
# Stage changes (add to snapshot)
git add main.c
git add . # Add all files
# Create a snapshot (commit)
git commit -m "feat: Add user login feature"
# Upload to remote
git push origin main
# Download updates
git pull origin main
実行結果(git status)
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged ..." to unstage)
        modified:   main.c
    

ブランチ(並行開発)

ブランチを使うと、メインのコードに影響を与えずに新機能を開発できます。

branching.sh
# Create and switch to a new branch
git switch -c feature/user-auth
# Work on your feature...
# (edit files, test, etc.)
# Switch back to main
git switch main
# Merge your feature
git merge feature/user-auth
# Delete the branch (cleanup)
git branch -d feature/user-auth

合格ライン

git init, clone, status を説明できる
add, commit, push の流れを実行できる
ブランチを作成・マージできる
コミットメッセージを適切に書ける

参考リンク

Pro Git(日本語版) — 公式の無料書籍。網羅的。
GitHub Docs — GitHub公式ドキュメント。
Conventional Commits — コミットメッセージ規約。

演習課題

課題1: 初めてのリポジトリ
新しいフォルダを作成し、git init でリポジトリを作成してください。その後、README.md を作成してコミットしてください。
課題2: コミットメッセージ
異なる変更を3回コミットし、それぞれに Conventional Commits 形式のメッセージ (feat:, fix:, docs:) をつけてください。

次のステップ