LoginSignup
1
5

More than 3 years have passed since last update.

直前にpushしたコミットメッセージを変更

Posted at

はじめに

こんにちは。突然ですが皆さんはこのような経験がありませんか?

"コミットメッセージをしっかり決めずにpushしちゃった!"
"コミットメッセージにissue番号つけ忘れた!"
"コミットメッセージにadd/modifyの文字をつけ忘れた!"

誰しもとは言いませんが、pushしたコミットメッセージを編集したい時はありますよね、、、
自分は何回かあったので記事にしようとと。

① まずはgit log --onlineにて修正したコミットを確認

$ git log --online

faaa223 [add]投稿詳細ページの作成 #39    //一番新しいコミット
b79cf0e [modify]投稿一覧ページ修正 #38
640522e [modify]新規投稿ページ修正    //修正したいコミット //issue番号をつけ忘れた!

今回は例として3番目のコミットを修正していきます。

② git rebase -iにて内容を修正

ここで登場するのがgit rebase -iコマンド
一番最新のコミットが1として今回変更したいコミットは3個前なので3。"HEAD~"の前に3を入れます。

$ git rebase -i HEAD~3

そうするとviエディタが起動します。

③ viエディタにて編集

先程のコマンドを打つとこのような画面が出てきます。しかしこのままでは文字が打てないので
i を打ちINSERTモードにします。(画面下部にINSERTと表示されます)

pick faaa223 [add]投稿詳細ページの作成 #39    
pick b79cf0e [modify]投稿一覧ページ修正 #38
pick 640522e [modify]新規投稿ページ修正

# Rebase 86abff0..33d7b8d onto 86abff0 (2 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 <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# 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>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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

--INSERT--   //iを打って編集モードになると表示される。

変更したいコミットのpickをeに変更

pick faaa223 [add]投稿詳細ページの作成 #39    
pick b79cf0e [modify]投稿一覧ページ修正 #38
e 640522e [modify]新規投稿ページ修正         //pickをeに変更

# Rebase 86abff0..33d7b8d onto 86abff0 (2 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 <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# 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>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# 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

--INSERT--   //iを打って編集モードになると表示される。

eに変更したらESCキーを押してコマンドモードに切り替えます。(コマンドモードに切り替えるとINSERTが消えます)

:wqを押して上書き保存して終了。

④ git commit --amend にてコミットメッセージを変更

$ git commit --amend -m "[modify]新規投稿ページ修正 #37"    //issue番号を追加

これでコミットメッセージの変更が完了になります。

⑤ git rebase --continue

$ git rebase --continue 

もし他にeに変更したコミットがある場合は順次移動します

⑥ git log --onelineにて確認

$ git log --oneline

faaa223 [add]投稿詳細ページの作成 #39    
b79cf0e [modify]投稿一覧ページ修正 #38
640522e [modify]新規投稿ページ修正 #37   //修正完了

⑦ git push -f origin ブランチ名 にて強制push

$ git push -f origin ブランチ名

-fにて強制pushしてリモート上でメッセージ変更完了

強制pushを行うときは、想定外のことが起こる可能性もあるので注意!(チーム開発をしていたり。)

1
5
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
5