Git入門
コードの変更履歴を記録し、いつでも過去に戻れる分散型バージョン管理システム。
用語集
リポジトリ
プロジェクトの保管庫。履歴も含む。
コミット
スナップショット。セーブポイント。
ブランチ
分岐。並行開発の単位。
マージ
ブランチを統合する操作。
リモート
GitHub等のサーバー上のリポジトリ。
なぜGitが必要か
Gitがないと、ファイル名に日付やバージョン番号をつけて管理することになります。
Bad
# Bad: Meaningless commit messagesgit commit -m "fix"git commit -m "update"git commit -m "asdf"Good
# 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:)を使うと履歴が読みやすくなる。
基本コマンド
# Create a new repositorygit init my_project
# Clone an existing repositorygit clone https://github.com/user/repo.git
# Check current statusgit status
# Stage changes (add to snapshot)git add main.cgit add . # Add all files
# Create a snapshot (commit)git commit -m "feat: Add user login feature"
# Upload to remotegit push origin main
# Download updatesgit 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
ブランチ(並行開発)
ブランチを使うと、メインのコードに影響を与えずに新機能を開発できます。
# Create and switch to a new branchgit switch -c feature/user-auth
# Work on your feature...# (edit files, test, etc.)
# Switch back to maingit switch main
# Merge your featuregit merge feature/user-auth
# Delete the branch (cleanup)git branch -d feature/user-auth合格ライン
git init, clone, status を説明できる
add, commit, push の流れを実行できる
ブランチを作成・マージできる
コミットメッセージを適切に書ける
参考リンク
演習課題
課題1: 初めてのリポジトリ
新しいフォルダを作成し、git init でリポジトリを作成してください。その後、README.md を作成してコミットしてください。
課題2: コミットメッセージ
異なる変更を3回コミットし、それぞれに Conventional Commits 形式のメッセージ (feat:, fix:, docs:) をつけてください。