Git is a “time machine” for your files: you save small snapshots called commits, try ideas safely on branches, and share your work by pushing to a remote like GitHub.
Gitは「作業のタイムマシン」。ファイルをちょっとずつ写真(スナップショット)のように保存し、失敗しても戻れます。分岐(ブランチ)で安全に試せて、インターネット上の置き場(GitHubなど)で共有できます。
main
に影響を出さず別道(ブランチ)で実験正確には:Gitは内容に基づく履歴管理を行い、コミットは親コミットへの参照とスナップショットを持つ有向グラフを作ります。ブランチは先頭コミットへの軽量ポインタ、リモートは別のリポジトリURLです。
Git Bash
# 1回だけの初期設定(名前とメール)
git config --global user.name "あなたの名前"
git config --global user.email "[email protected]"
# 新規 or 既存から
git init # いまのフォルダをGit管理にする
# or
git clone https://github.com/user/repo.git # 既存をコピー
Git Bash
git status # 変更の確認
git add index.html # 写す変更を選ぶ(ステージング)
git commit -m "初回の保存" # 写真(コミット)を撮る
git log --oneline # 履歴を見る
ポイント:git add
で「写すもの」を選び、git commit
で「撮影」します。
Git Bash
git switch -c feature/header # 新しい道を作って移動
# 変更して…
git add .
git commit -m "ヘッダー追加"
git switch main # 元の道へ
git merge feature/header # 変更を取り込む(必要なら)
git checkout
でもOKですが、初心者は git switch
/git restore
が分かりやすいです。
Git Bash
git restore path/to/file
Git Bash
git restore --staged path/to/file
Git Bash
git commit --amend
Git Bash
git reset --hard HEAD~1
共同作業中は reset --hard
を避け、revert
で取り消しコミットを作る方が安全です。
Git Bash
git remote add origin https://github.com/user/repo.git
git push -u origin main # はじめての送信(上流設定つき)
git pull # みんなの変更を取り込む
小ワザ:初回は -u
を付けると次回以降 git push
だけでOK。
ビルド成果物や秘密鍵は履歴に入れないで!
リポジトリ直下に .gitignore
を置き、例:
Git Bash
node_modules/
dist/
*.log
.env
fix: スマホ表示でヘッダーが崩れる不具合を修正
commit
が必要.gitignore
restore
、共同なら revert