Git を使う際の頻出コマンドや設定、概念を整理したメモ。
トラブル時の参照や、作業の抜け漏れ防止を目的とする。
用語の簡単な説明(最初に押さえておきたい基礎)
- リポジトリ(Repository):プロジェクトの履歴を保存する場所。
- ワーキングツリー(Working Tree):実際に作業しているファイル群。
- ステージングエリア(Index):コミット前に変更を一時的に置く場所。
- コミット(Commit):変更のスナップショット。
- ブランチ(Branch):作業の流れを分岐させる仕組み。
- HEAD:現在の作業位置を指すポインタ。
- origin:デフォルトのリモートリポジトリ名。
- upstream:追跡対象のリモートブランチ。
- bare リポジトリ:作業ツリーを持たないリポジトリ(中央管理用)。
- fast-forward:履歴が直線的に進むマージ。
- no‑ff:必ずマージコミットを作るマージ。
- squash:複数コミットを 1 つにまとめる。
- rebase:履歴を書き換えて直線化する。
- cherry-pick:特定のコミットだけを取り込む。
Git コマンドまとめ
1. リポジトリ操作
リポジトリの初期化
git init
- 意味:カレントディレクトリを Git 管理下に置く
.git/が作成される
bare リポジトリの作成
git init --bare <repo>.git
- --bare:作業ツリーなしのリポジトリ
- 用途:中央リポジトリとして利用(GitHub のような役割)
リポジトリの取得
git clone <URL> [dir]
<URL>:リモートリポジトリ[dir]:任意のディレクトリ名
2. 状態確認・差分確認
状態確認
git status
- 変更状況(未追跡・変更済み・ステージ済み)を確認
差分確認
git diff
git diff --staged
git diff <branch1>..<branch2>
- --staged:ステージ済みの差分
<branch1>..<branch2>:ブランチ間の差分
履歴確認(視覚的)
git log --oneline --graph --decorate --all
- --oneline:1行表示
- --graph:ブランチ構造
- --decorate:タグ・ブランチ名
- --all:全ブランチ表示
3. コミット操作
ステージング
git add <file>
git add .
.:カレント以下すべて
コミット
git commit -m "message"
- -m:メッセージ指定
直前のコミットを修正
git commit --amend
- --amend:直前のコミットを上書き
- 用途:メッセージ修正・変更追加
- 注意:push 済みコミットには使わない
4. 取り消し・やり直し
変更を破棄(ステージ前)
git restore <file>
ステージングを解除
git restore --staged <file>
reset(履歴を書き換える)
git reset --soft HEAD~1
- --soft:コミットだけ取り消し、変更は残す
git reset --mixed HEAD~1
- --mixed(デフォルト):コミット取り消し+ステージ解除
git reset --hard HEAD~1
- --hard:変更もコミットも完全に破棄
- 注意:取り返し不可
revert(履歴を壊さずに取り消す)
git revert <commit>
- 指定コミットを打ち消す新しいコミットを作成
5. ブランチ操作
ブランチ一覧
git branch
git branch -a
- -a:ローカル+リモートすべて表示
ブランチ切り替え
git switch <branch>
ブランチ作成+切り替え
git switch -c <branch>
- -c:create(作成)
6. マージ(fast-forward / no‑ff / squash)
通常のマージ
git merge <branch>
fast-forward マージ
git merge --ff-only <branch>
- --ff-only:fast-forward 可能な場合のみマージ
- 履歴が直線的に保たれる
fast-forward を禁止(no‑ff)
git merge --no-ff <branch>
- --no-ff:必ずマージコミットを作成
- 履歴に「マージした」という痕跡を残す
スカッシュマージ
git merge --squash <branch>
git commit -m "Squashed commit"
- --squash:複数コミットを 1 つにまとめる
- マージコミットは作られない(手動 commit 必須)
7. リベース(rebase / interactive rebase)
通常のリベース
git rebase <branch>
- 指定ブランチの最新履歴を取り込む
- 履歴が直線的になる
インタラクティブリベース
git rebase -i HEAD~5
- -i:interactive(対話的)
- 操作一覧:
pick:そのままsquash:前のコミットにまとめるreword:メッセージ変更drop:削除
8. cherry-pick(特定コミットだけ取り込む)
git cherry-pick <commit>
- 特定コミットだけを現在のブランチに取り込む
- hotfix の反映などに便利
9. stash(作業の一時退避)
git stash
git stash save "message"
git stash list
git stash apply stash@{0}
git stash drop stash@{0}
- apply:適用するが stash は残る
- pop:適用して stash を削除
10. GitHub / リモート操作
リモート設定
git remote add origin <URL>
git remote -v
push
git push origin main
git push -u origin main
- -u:upstream 設定(次回以降
git pushだけでOK)
pull
git pull
git pull --rebase
- --rebase:履歴を直線的に保つ
11. 設定(config)
ユーザー設定
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
エディタ設定
git config --global core.editor "code --wait"
差分カラー
git config --global color.ui auto
12. トラブルシュート
プッシュできない(reject)
git pull --rebase
git push
コンフリクト発生
git status
git add .
git rebase --continue
間違えて消した
git reflog
- 過去の状態に戻れる