1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

gitコミット時に間違ったコメントを直す

Posted at

まえおき

UdemyでVue.jsの講座を受けつつ、レッスン単位でコミットしていた。
が、あるコミットからコメントが間違っていた。
直前にコミットならサクっと修正できるけど、ちょっと前から連続している…。
この記事はそんなやらかしと修正の記録である。

状況

当該リポジトリでgit log --oneline叩くと以下のようになる。

terminal
% git log --oneline
f079f8e (HEAD -> master) lesson1-5 end // ここで間違っているのに気がついた
6317475 lesson1-4 end
85155b7 lesson1-3 end
29c35b0 lesson1-2 end
292fa3b lesson1-1 end // ここからlesson2-xにするつもりだった
8b895d5 lesson1-2 end
37ec038 lesson1-1 end

やり方

  1. どのコミットまで戻すか決定する
  2. コミットごとにコメントを修正して、コミットする
  3. すべてのコミットを修正して、git pushして完了

1.どのコミットまで戻すか決定する

目的のコミット(292fa3b)はHEADから数えて何番目か数える。
数える際は、HEADを1番目とするので、目的のコミットは5番目ということになる。

terminal
% git log --oneline
f079f8e (HEAD -> master) lesson1-5 end // 1番目
6317475 lesson1-4 end // 2番目
85155b7 lesson1-3 end // 3番目
29c35b0 lesson1-2 end // 4番目
292fa3b lesson1-1 end // 5番目
8b895d5 lesson1-2 end
37ec038 lesson1-1 end

2.コミットごとにコメントを修正する

目的のコミットが何番目か確認したら、git rebaseを叩く。
この時、-iオプションを設定し、HEADから何番目かをチルダの後に示す。

terminal
% git rebase -i HEAD~5    //HEADから5番目なので、チルダの後ろは5

すると以下のようにエディタが表示され、コミットごとに何をするか選択可能になる。
(エディタの操作方法については秀逸な記事がいくらでも転がっているので探してください)

pick 292fa3b lesson1-1 end
pick 29c35b0 lesson1-2 end
pick 85155b7 lesson1-3 end
pick 6317475 lesson1-4 end
pick de3921c lesson1-5 end

# Rebase 8b895d5..de3921c onto 8b895d5 (5 commands)
#
# Commands:
(以下略)

1コミットが1行として表されている。
編集したいコミットの行頭をpickからeditに書き換えると、修正可能となる。
上記をeditに書き換えた例は以下。

edit 292fa3b lesson1-1 end
edit 29c35b0 lesson1-2 end
edit 85155b7 lesson1-3 end
edit 6317475 lesson1-4 end
edit de3921c lesson1-5 end

# Rebase 8b895d5..de3921c onto 8b895d5 (5 commands)
#
# Commands:
(以下略)

書き込みを行ってエディタから抜けると、さっそく修正が行える状態になる。
下記例では、エディタから抜けた瞬間

% git rebase -i HEAD~5
Stopped at 292fa3b...  lesson1-1 end
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue
% git commit --amend -m "lesson2-1 end"
[detached HEAD 2693e0b] lesson2-1 end
 Date: Thu Jun 11 17:35:56 2020 +0900
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 lesson2.txt
% git log --oneline
2693e0b (HEAD) lesson2-1 end // 修正された
8b895d5 lesson1-2 end
37ec038 lesson1-1 end

コメントを変えたら次のコミットを修正するので、git rebase --continueで次のコミットに移動する。

% git rebase --continue
Stopped at 29c35b0...  lesson1-2 end

この後、Stopped at 〜で現在対象とするコミットが示されるので、「適切なコメントに修正→次のコミットに移動」を繰り返す。
最後まで修正すると、以下が表示される。

% git rebase --continue
Successfully rebased and updated refs/heads/master.

以上で修正は完了。おつかれさまでした。

おまけ:直前のコメントを修正したい

git commit--amendを付けて正しいメッセージを付与する

terminal
% git log --oneline
fg79f8e (HEAD -> master) message2 // <- message3にするつもりが間違えた
9217475 message 2
85729b7 message 1

% git commit --amend -m "message3"

% git log --oneline
hg7g07e (HEAD -> master) message3 // <- 修正OK
9217475 message 2
85729b7 message 1
1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?