Git の高度なコマンド: Stash、Reflog、Bisect
基本的なコマンド (コミット、プッシュ、マージ) に加えて、Git はワークフローを管理するための高度なツールを提供します。 複合体を修復し、失われた作業を回復し、問題をデバッグします。 Git スタッシュ 作業を節約します 一時的な、 レフログ それは回復のための命の恩人です、そして 二等分する 二分探索でバグを見つけます。これらのコマンドをマスターすれば、Git パワー ユーザーになれます。
🎯 何を学ぶか
- 一時的な作業を保存するための Git stash
- 「失われた」コミットを回復するための Git reflog
- Git bisect でバグを引き起こしたコミットを見つける
- ワークツリーとサブモジュール (ボーナス)
Git Stash: 一時的なメモ帳
git stash コミットされていない変更を一時的な「スタッシュ」に保存し、クリーンアップします
作業ディレクトリ。ブランチをすぐに変更する必要があるが、現在のジョブでは変更できない場合に便利です。
コミットの準備ができています。
# Situazione: stai lavorando su feature ma arriva hotfix urgente
git status
# modified: src/component.ts
# modified: src/style.css
# Salva i cambiamenti in stash
git stash
# Output: Saved working directory and index state WIP on feature: abc1234 last commit
# Working directory ora è pulito
git status
# nothing to commit, working tree clean
# Cambia branch e lavora sull'hotfix
git checkout main
# ... lavoro su hotfix ...
# Torna al feature branch
git checkout feature
# Recupera i cambiamenti da stash
git stash pop
# Riapplica i cambiamenti e rimuove lo stash
高度な隠し場所
# Stash con messaggio descrittivo
git stash save "WIP: refactoring authentication"
# Stash includendo file untracked
git stash -u
# Stash includendo tutto (anche .gitignore)
git stash -a
# Lista tutti gli stash
git stash list
# stashHEAD@{{ '{' }}0{{ '}' }}: WIP on feature: abc1234 refactoring auth
# stashHEAD@{{ '{' }}1{{ '}' }}: On main: def5678 emergency fix
# stashHEAD@{{ '{' }}2{{ '}' }}: WIP on develop: ghi9012 new feature
# Vedere contenuto di uno stash
git stash show stashHEAD@{{ '{' }}0{{ '}' }}
git stash show -p stashHEAD@{{ '{' }}0{{ '}' }} # Con diff completo
# Applicare stash senza rimuoverlo
git stash apply stashHEAD@{{ '{' }}1{{ '}' }}
# Rimuovere stash specifico
git stash drop stashHEAD@{{ '{' }}0{{ '}' }}
# Rimuovere tutti gli stash
git stash clear
# Creare branch da stash
git stash branch new-feature-branch stashHEAD@{{ '{' }}0{{ '}' }}
部分的な隠し場所
一部のファイルのみを stash に保存し、他のファイルは作業ディレクトリに保存することができます。
# Stash interattivo: scegli cosa salvare
git stash -p
# Per ogni hunk:
# y = stash this hunk
# n = do not stash this hunk
# q = quit
# a = stash this and all remaining hunks
# d = do not stash this or any remaining hunks
# Stash solo file specifici
git stash push -m "Save only component" src/component.ts
# Stash tutto tranne file specifici (path spec)
git stash push -- '*.ts' '!src/test.ts'
Git Reflog: あなたの命の恩人
git reflog 記録 頭のあらゆる動き削除されたコミットであっても、
リセット、リベースが中止されました。すべてを失ったと思ったときに救ってくれるのはバックアップです。
# Vedere reflog di HEAD
git reflog
# abc1234 HEADHEAD@{{ '{' }}0{{ '}' }}: commit: fix bug
# def5678 HEADHEAD@{{ '{' }}1{{ '}' }}: checkout: moving from main to feature
# ghi9012 HEADHEAD@{{ '{' }}2{{ '}' }}: reset: moving to HEAD~1
# jkl3456 HEADHEAD@{{ '{' }}3{{ '}' }}: commit: add feature
# mno7890 HEADHEAD@{{ '{' }}4{{ '}' }}: commit: initial commit
# Reflog di branch specifico
git reflog show feature
# Reflog con date
git reflog --date=relative
git reflog --date=iso
Reflog による回復
reflog が作業内容を保存する方法の実際的な例:
# Hai fatto reset e perso commit
git reset --hard HEAD~3
# Oh no! Volevo solo HEAD~1
# Trova i commit persi nel reflog
git reflog
# abc1234 HEADHEAD@{{ '{' }}0{{ '}' }}: reset: moving to HEAD~3
# def5678 HEADHEAD@{{ '{' }}1{{ '}' }}: commit: important feature ← VOGLIO QUESTO!
# Recupera il commit
git reset --hard def5678
# Oppure
git reset --hard HEADHEAD@{{ '{' }}1{{ '}' }}
# Commit recuperati!
# Cancelli branch per sbaglio
git branch -D feature-important
# Deleted branch feature-important (was abc1234).
# Oh no! Serve recuperarlo
git reflog
# Trova l'ultimo commit del branch cancellato
# abc1234 HEADHEAD@{{ '{' }}5{{ '}' }}: commit: last commit on feature
# Ricrea il branch
git branch feature-important abc1234
# Branch recuperato!
# Rebase va male, abortisci ma perdi lavoro
git rebase --abort
# Trova lo stato prima del rebase
git reflog
# def5678 HEADHEAD@{{ '{' }}1{{ '}' }}: rebase: aborting ← stato corrotto
# ghi9012 HEADHEAD@{{ '{' }}2{{ '}' }}: checkout: moving from feature to main
# abc1234 HEADHEAD@{{ '{' }}3{{ '}' }}: commit: my work ← STATO BUONO
# Torna allo stato pre-rebase
git reset --hard abc1234
Git Bisect: バイナリ検索によるデバッグ
git bisect バイナリ検索を使用して、どのコミットがバグを引き起こしたかを見つけます。
bisect は、何百ものコミットを手動でチェックする代わりに、log(n) ステップで原因を見つけます。
# Situazione: Il bug esiste ora (HEAD) ma non esisteva 10 giorni fa
# Hai 100 commit in mezzo: quale ha introdotto il bug?
# 1. Inizia bisect
git bisect start
# 2. Marca commit corrente come "bad" (ha il bug)
git bisect bad
# 3. Marca commit vecchio come "good" (non aveva il bug)
git bisect good abc1234
# Git fa checkout del commit a metà
# Bisecting: 50 revisions left to test after this
# 4. Testa se il bug esiste
npm test
# Se test fallisce:
git bisect bad
# Se test passa:
git bisect good
# 5. Ripeti fino a che Git trova il commit colpevole
# Bisecting: 25 revisions left...
# Bisecting: 12 revisions left...
# Bisecting: 6 revisions left...
# Bisecting: 3 revisions left...
# Bisecting: 1 revision left...
# def5678 is the first bad commit
# commit def5678
# Author: Developer <dev@example.com>
# Date: Mon Dec 1 10:00:00 2024
#
# refactor: change authentication logic ← QUESTO È IL COLPEVOLE
# 6. Fine bisect
git bisect reset
# HEAD torna al branch originale
自動二分化
テスト スクリプトを使用して二等分を自動化できます。
# test.sh - script che ritorna 0 se OK, 1 se bug presente
#!/bin/bash
npm test | grep "PASS"
exit $?
# Bisect automatico
git bisect start
git bisect bad HEAD
git bisect good abc1234
git bisect run ./test.sh
# Git testa automaticamente ogni commit e trova il colpevole!
# Output finale:
# def5678 is the first bad commit
Git Worktree: 複数の同時ブランチ
git worktree 複数のブランチを同時にチェックアウトできます
異なるディレクトリ。現在のジョブを一時保存せずにホットフィックスに取り組む場合に便利です。
# Situazione: stai su feature branch, serve urgente hotfix
# Invece di stash, crea nuovo worktree
# Crea worktree per hotfix
git worktree add ../hotfix-worktree hotfix/critical-bug
# Ora hai due directory:
# ./project (feature branch)
# ../hotfix-worktree (hotfix branch)
# Vai alla directory hotfix
cd ../hotfix-worktree
# Lavora sul fix
git commit -m "fix: critical bug"
git push
# Torna al progetto principale
cd ../project
# Continua lavoro su feature senza aver toccato nulla
# Lista worktree
git worktree list
# /path/to/project abc1234 [feature]
# /path/to/hotfix-worktree def5678 [hotfix/critical-bug]
# Rimuovi worktree quando finito
git worktree remove ../hotfix-worktree
ベストプラクティス
✅ 隠し場所
- 説明的なメッセージを使用します。
git stash save "descrizione" - スタッシュを数か月間放置しないでください (コミットまたは破棄)
- アメリカ合衆国
git stash list定期的に掃除のために
✅ リブログ
- reflog をチェックする 前に 危険なリセット/リベースの数
- Reflog はローカルであり、プッシュされません
- Reflog は 90 日後に自動的にクリーンアップします (構成可能)
✅ 二等分
- スクリプトを使用して自動化して速度を上げる
- 「良い」コミットが本当に良いものであることを確認してください
- アメリカ合衆国
git bisect skipコミットがコンパイルされない場合
ボーナスコマンド
# Git fsck: verifica integrità repository
git fsck
# Trova oggetti corrotti o dangling
# Git gc: garbage collection (pulizia)
git gc --aggressive
# Git clean: rimuovi file untracked
git clean -fd # -f force, -d directories
git clean -n # Dry run (vedi cosa verrebbe rimosso)
# Git archive: crea archive del codice
git archive --format=zip HEAD > code.zip
# Git blame: vedi chi ha scritto ogni riga
git blame file.ts
git blame -L 10,20 file.ts # Solo righe 10-20
# Git shortlog: summary di contributi
git shortlog -sn # -s summary, -n ordinato per numero
結論
Git の高度なコマンドは、困難な状況を解決可能な問題に変えます。 隠し場所 臨時の仕事を管理し、 レフログ コミットを取得する 「失われた」、e 二等分する バグを効率的に見つけます。これらのツールを知る 恐れることなく探索し、実験し、間違いから回復する自信を与えてくれます。
🎯 重要なポイント
- Stash は一時的な作業を保存します。
git stash/git stash pop - Reflog はすべてをログに記録します。
git reflog回復のために - Bisect は二分探索でバグを見つけます。
git bisect start/good/bad - 複数の同時ブランチのためのワークツリー
- これらのコマンドは危機的な状況からあなたを救います







