1
3

More than 3 years have passed since last update.

git 過去の複数コミットのコメントを修正する

Posted at

これはなに

過去の複数のコミットのコメントを一斉に修正した際のメモ。

考え方

git rebase を使用して、既存のコミットを新規コミットで置き換える際に、コメントを書き換える。

手順

まず、修正するコミットのハッシュを調査する。

$ git log --first-parent <branch-name>

修正したいコミットの、ひとつ前のコミットのハッシュを調べる。

つぎに、git rebase を開始する。

$ git rebase -i <target-hash>

指定した hash の次以後が rebase の対象となる。
標準 editor で以下のような編集画面が開く。

pick d66d1fc3a feat: define placeholder at patients
pick fa93d61e7 feat: define placeholder at groups
pick 4a015ad56 wip: rem placeholder
pick 73c8ae6a4 feat: add place holder

# Rebase d860920d9..73c8ae6a4 onto d860920d9 (4 commands)
#
# Commands:
# p, pick = 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 = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out

ここで、修正したいコミットの pickedit に変更し save する。
もし行を消去すると、該当行はコミットから除外されるので注意。

editreword の違いについて(たしかこんな感じだった)
- edit は、コミット毎に amend が実行され、コメントを編集する。
- reword は、対象コミットのすべてのコメントを(同一の内容で)置き換える

そして、edit に修正したコミットの編集を開始する。
git commit --amend を実行する。

$ git commit --amend

コミットを編集し、save する。
つぎのコミットを編集するため、git rebase --continue する

$ git rebase --continue

前述と同様に、git commit --amendを実施する。
これを edit に変更した全コミット分繰り返す。
全コミットに分完了すると、git rebase の完了となる。

$ git rebase --continue
Successfully rebased and updated refs/heads/fix/10541_hoge_fuga.

つぎに、この変更した branch をリモート(origin)に反映する。

$ git push -f origin <branch-name>

-f オプションを付与し、強制的に更新する。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3