大量のコミットを git rebase -i でまとめたいときに便利な Vim 操作手順をご紹介します。
pick を fixup に置き換える作業を効率化できます。
$ git rebase -i origin/main
ここで開かれる Vim の画面でコミットを操作します。
pick a1b2c3d WIP: add user model
pick d4e5f6g fix: typo in variable name
pick h7i8j9k add user controller
pick l0m1n2o implement user service
pick p3q4r5s add tests for user service
pick t6u7v8w update README
# Rebase 1234567..t6u7v8w onto 1234567 (6 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec <cmd> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop = 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>]
# create a merge commit using the original merge commit's
# message (or the oneline, if no original merge commit was
# specified)
#
# These lines can be re-ordered; they are executed from top to bottom.
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
pick を fixup に変更したい場合を例とします。
※ fixup と入力する時は f と省略しても良いです。
方法1: 1行ずつ確認して置換
-
j/kで上下に移動して、対象のコミット行までカーソルを移動する - カーソルを
pickの行頭にあることを確認する-
0で行頭へ移動
-
-
cwfixup<Esc>と入力-
cwで単語削除 -
fixupと入力 -
<Esc>で確定
-
- 次の対象に移動(
+)-
+で次の行の行頭へ移動
-
-
.(ドット)で直前の操作を繰り返す
方法2: 対話的にまとめて置換
-
:%s/pick/fixup/c-
replace with fixup (y/n/a/q/l/^E/^Y)?と毎行質問される-
y置換する -
n置換しない(skip) -
a以降すべて置換 -
q中断
-
-
コミットごとに選びながら置換したい場合におすすめです。