Git インタラクティブ リベースとは
Il gitインタラクティブリベース (対話型リベース) は最も強力なツールの 1 つです Git の コミット履歴を書き換える。再編成、変更、 作業を共有する前にコミットを結合または削除し、クリーンな履歴を作成し、 コードレビューとデバッグを容易にするプロフェッショナル。
🎯 インタラクティブなリベースでできること
- スカッシュ: 複数のコミットを 1 つに結合する
- 言い換え: コミットメッセージを編集する
- 編集: コミットの内容を変更する
- 再注文: コミットの並べ替え
- ドロップ: 不要なコミットを削除する
- 修正: メッセージを破棄してコミットを結合する
⚠️ 黄金律: パブリックコミットを決してリベースしないでください!
Rebase は Git 履歴を書き換えます。 すでにブランチにプッシュされているコミットをリベースしない 共有 - すべての協力者に問題が発生する可能性があります。コミット時にのみリベースを使用する マージする前に、ローカルまたは個人の機能ブランチ上で。
インタラクティブなリベースを開始する
対話型リベースを開始するには、リベースするバックコミットの数を指定します。
git rebase -i HEAD~N ここで、N はコミット数です。
# Riscrivere gli ultimi 3 commit
git rebase -i HEAD~3
# Rebase tutti i commit dal branch main
git rebase -i main
# Rebase da uno specifico commit (escluso)
git rebase -i abc1234
# Vedere quali commit saranno inclusi prima di rebase
git log --oneline HEAD~5..HEAD
インタラクティブエディター
インタラクティブなリベースを開始すると、Git は設定済みのテキスト エディターを開き、 コミットと利用可能なオプションのリスト:
pick abc1234 Add user authentication
pick def5678 Fix typo in auth module
pick ghi9012 Add password validation
pick jkl3456 Update tests for auth
# Rebase 0123456..jkl3456 onto 0123456 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
一般的な対話型リベース操作
1. スカッシュ: コミットの結合
潰す 複数のコミットを 1 つに結合し、統合に最適です WIP コミット (進行中の作業) または論理的で完全なコミットの小さな修正。
# Storia originale
pick abc1234 Add login form
pick def5678 Fix button styling
pick ghi9012 Add validation
pick jkl3456 Fix validation bug
# Modifica in:
pick abc1234 Add login form
squash def5678 Fix button styling
squash ghi9012 Add validation
squash jkl3456 Fix validation bug
# Risultato: 1 commit che combina tutti e 4
保存後、Git は新しいコミット メッセージを作成するように求めます。
# This is a combination of 4 commits.
# This is the 1st commit message:
Add login form
# This is the commit message #2:
Fix button styling
# This is the commit message #3:
Add validation
# This is the commit message #4:
Fix validation bug
# Puoi modificare in un messaggio unico più chiaro:
feat: implement user login with form validation
- Created login form component
- Added email and password validation
- Styled form buttons
- Fixed validation edge cases
スカッシュの前:
abc1234 Add login form
def5678 Fix button styling
ghi9012 Add validation
jkl3456 Fix validation bug
スカッシュ後:
xyz7890 feat: implement user
login with validation
2. 修正: メッセージなしでスカッシュする
fixup スカッシュに似ていますが、コミットメッセージを破棄し、メッセージのみを保持します。
前回のコミットからのメッセージ。中間メッセージが必要ない場合は高速になります。
pick abc1234 Add user profile page
fixup def5678 Fix typo
fixup ghi9012 Fix linting errors
fixup jkl3456 Remove console.log
# Risultato: 1 commit con messaggio "Add user profile page"
# I messaggi degli altri 3 sono scartati automaticamente
💡 プロのヒント: git commit --fixup
コミットするときに使用できます --fixup 以前のコミットからの修正としてマークするには:
# Durante lo sviluppo
git commit -m "Add feature X"
# ... lavori ...
git commit --fixup abc1234 # abc1234 è l'hash del commit da fixare
# Poi quando fai rebase
git rebase -i --autosquash HEAD~5
# Git automaticamente organizza i fixup commits!
3. 言い換え: メッセージを編集する
変化 pick in reword コミットメッセージのみを編集するには、
内容には触れずに。
pick abc1234 Add user auth
reword def5678 Fixed bug
pick ghi9012 Update tests
# Quando salvi, Git apre l'editor per def5678
# e puoi cambiare "Fixed bug" in qualcosa di più descrittivo:
fix: resolve authentication timeout issue
Fixed a race condition that caused authentication
to timeout under heavy load.
4. 編集: コミット内容を編集します。
アメリカ合衆国 edit リベースを停止し、コミットを変更できるようにします
(ファイルの追加/削除、コードの編集)。
pick abc1234 Add payment module
edit def5678 Add validation
pick ghi9012 Add tests
# Quando arrivi a def5678, il rebase si ferma
# Ora puoi modificare file
vim src/validation.ts
# Aggiungi le modifiche al commit
git add src/validation.ts
git commit --amend --no-edit
# Oppure crea nuovi commit
git commit -m "Additional changes"
# Continua il rebase
git rebase --continue
5. ドロップ: コミットをドロップ
変化 pick in drop (または行を削除) を削除するには
完全に歴史からのコミットです。
pick abc1234 Add feature X
drop def5678 Add debug logging
pick ghi9012 Add feature Y
# Oppure semplicemente elimina la riga:
pick abc1234 Add feature X
pick ghi9012 Add feature Y
# Il commit def5678 sparisce dalla storia
6. 並べ替え: コミットの並べ替え
エディターで行を移動するだけで、コミットの順序を変更できます。
# Ordine originale
pick abc1234 Add tests
pick def5678 Add feature
pick ghi9012 Add documentation
# Riordina per logica migliore
pick def5678 Add feature
pick abc1234 Add tests
pick ghi9012 Add documentation
# I commit vengono riapplicati nel nuovo ordine
⚠️衝突に注意してください
コミットが相互に依存している場合、コミットの順序を変更すると競合が発生する可能性があります。 コミットによって、後のコミットによって追加されたコードが変更されると、競合が発生します。
コミットを分割する
必要な場合もあります 大きなコミットを分割する より小さく、より焦点を絞ったコミットに分割します。
アメリカ合衆国 edit e git reset.
# Avvia rebase e marca il commit da dividere con 'edit'
git rebase -i HEAD~3
# Quando il rebase si ferma al commit da dividere:
# 1. Resetta il commit (mantiene i cambiamenti come unstaged)
git reset HEAD^
# 2. Ora hai tutti i cambiamenti come uncommitted
git status
# 3. Crea commit più piccoli e focalizzati
git add src/user.model.ts
git commit -m "Add user model"
git add src/user.service.ts
git commit -m "Add user service"
git add src/user.component.ts
git commit -m "Add user component"
# 4. Continua il rebase
git rebase --continue
# Risultato: 1 commit diviso in 3 commit logici
エラーの管理と回復
進行中のリベースのキャンセル
# Se qualcosa va storto durante il rebase
git rebase --abort
# Torna allo stato precedente al rebase
リベース中の競合の解決
# Se incontri conflitti durante rebase
# Git si ferma e mostra i file in conflitto
# 1. Risolvi i conflitti nei file
vim conflicted-file.ts
# 2. Aggiungi i file risolti
git add conflicted-file.ts
# 3. Continua il rebase
git rebase --continue
# Se il conflitto è su un commit che vuoi skippare
git rebase --skip
問題が発生したリベースからの回復
# Git salva tutto nel reflog - puoi sempre recuperare
# Vedi la storia completa delle operazioni
git reflog
# Output esempio:
# abc1234 HEAD@{{ '{' }}0{{ '}' }}: rebase finished
# def5678 HEAD@{{ '{' }}1{{ '}' }}: rebase: checkout main
# ghi9012 HEAD@{{ '{' }}2{{ '}' }}: commit: Add feature
# Torna allo stato prima del rebase
git reset --hard HEAD@{{ '{' }}2{{ '}' }}
# Oppure crea un branch di backup
git branch backup-before-rebase HEAD@{{ '{' }}2{{ '}' }}
インタラクティブなリベースによるプロフェッショナルなワークフロー
📋 ベストプラクティスのワークフロー
1. 開発中: 頻繁なコミット
# Commit spesso con messaggi WIP
git commit -m "WIP: add login form"
git commit -m "WIP: fix styling"
git commit -m "WIP: add validation"
git commit -m "WIP: fix bug"
2. プッシュの前: 履歴のクリーニング
# Combina i WIP in commit logici
git rebase -i HEAD~10
# Squash tutti i WIP in:
# - feat: implement login with validation
# - test: add login form tests
# - docs: update authentication docs
3. プルリクエストの前: 最終レビュー
# Assicurati di avere base branch aggiornato
git fetch origin
git rebase -i origin/main
# Riordina se necessario
# Migliora messaggi di commit
# Rimuovi commit di debug/test
高度なコマンドとチート
--autosquash によるオートスカッシュ
# Durante sviluppo, marca i fixup
git commit --fixup abc1234
git commit --fixup def5678
# Al rebase, Git organizza automaticamente
git rebase -i --autosquash HEAD~10
# I commit fixup vengono automaticamente
# posizionati dopo il commit che fixano
Exec: リベース中にコマンドを実行する
pick abc1234 Add feature A
exec npm test
pick def5678 Add feature B
exec npm test
pick ghi9012 Add feature C
exec npm test
# Git esegue i test dopo ogni commit
# Se i test falliscono, il rebase si ferma
ルートリベース: 最初のコミットからリベース
# Rebase dall'inizio della storia
git rebase -i --root
# Utile per pulizia completa del repository
インタラクティブ リベースを使用すべきではない場合
❌ 避けるべき状況
- 公共ブランチ: メイン、開発、その他の共有ブランチをリベースしないでください。
- コミットはすでにプッシュされています: 他の人があなたのコミットに基づいた作業を行っている場合
- 複雑な競合時: マージはもっと簡単になるかもしれない
- 重要な話: 監査において正確な履歴が重要な場合
結論
Il gitインタラクティブリベース それはストーリーを維持するための非常に強力なツールです クリーンでプロフェッショナルな git。これを使用して、作業中のコミットを論理的で明確に説明されたコミットに統合します。 コードを共有する前に。常に覚えておいてください: リベースするのはローカル コミットのみであり、パブリック コミットは決して行わないでください。
🎯 インタラクティブなリベース チェックリスト
- ✅ プッシュされていないローカルコミットでのみ使用します
- ✅ 複雑なリベースの前にブランチバックアップ:
git branch backup - ✅ プッシュする前にコミット WIP/デバッグをスカッシュします
- ✅ スカッシュ後に明確なコミットメッセージを書き込む
- ✅ リベース後にコードをテストする
- ✅ 使用する
git reflog必要に応じて回復するため
📚 Git シリーズの次の作品
次の記事では、詳しく見ていきます Git リベースとマージ、 どの戦略をいつ使用するか、統合するベスト プラクティスを理解する プロチームの変化。







