🥺
【備忘録】今更ながらgit rebase -iを使用し感動した
Git Rebase 備忘録
お恥ずかしながら今まで実装に伴う試行錯誤の汚いコミットをあまり意識せずpushしていました。
最近もう一度体系的にgitの学習をしていて、今までなんとなく敬遠していたrebase -iを使用しコミットを整理したところめちゃくちゃ便利だったので同じような状況の方、ぜひrebase -iを使ってみてください。
基本コマンド
# 直近N個のコミットを編集
git rebase -i HEAD~N
# 特定のブランチから
git rebase -i ブランチ名
主要コマンド
pick (p) # そのまま使用
reword (r) # メッセージのみ変更
squash (s) # 前のコミットに統合(メッセージ保持)
fixup (f) # 前のコミットに統合(メッセージ破棄)
drop (d) # コミット削除
edit (e) # 内容を変更
よく使うパターン
1. コミットをまとめる
git rebase -i HEAD~3
# エディタで
pick abc1234 feat: add feature
fixup def5678 fix: typo
fixup ghi9012 fix: style
2. メッセージ修正
git rebase -i HEAD~1
# エディタで
reword abc1234 feat: add feature
3. 順序入れ替え
# エディタで順序を変更するだけ
pick ghi9012 fix: bug fix
pick abc1234 feat: add feature
注意
- プッシュ済みのコミットをrebaseしない
- 共有ブランチ(main)をrebaseしない
よくあるエラーと対処法
エラー1: "already a rebase-merge directory"
fatal: It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase.
原因: 前回のrebaseが途中で止まっている
対処法:
# 1. 前回のrebaseを中止
git rebase --abort
# 2. 前回のrebaseを完了
git rebase --continue
# 3. 前回のコミットをスキップ
git rebase --skip
# 4. コミットメッセージだけ変更
bashgit commit --amend
## よく使うコマンド一覧
```bash
# 現在の状態確認
git status
# rebase続行
git rebase --continue
# rebase中止
git rebase --abort
# 直前のコミット修正
git commit --amend
# 直前のコミット修正(メッセージそのまま)
git commit --amend --no-edit
まとめ
覚えておくポイント
- プッシュ前に整理、プッシュ後は触らない
- 困ったら
git rebase --abortで元に戻す
以上git rebase -iの紹介でした。
以下Udemyの講座がとても分かりやすかったので、gitについてもっと知りたい。再度学びなおしたい方はセール時に購入されてはいかがでしょうか。
Discussion