LoginSignup
1
1

More than 3 years have passed since last update.

【Git】git rebase -iでコミットをまとめる

Posted at

はじめに

適当なコメントをしたり、しょうもない修正をしてコミットしたい時ってありますよね〜〜〜
そんな悩みを解決!コミットを1つにまとめることができます。

git rebase -i

例えば、 以下のようにコミットが3つあったとします。

$ git log

commit ea86c0afbd528e265a1f0bde50307ebdbf41d857
Author: westhouseK <hogehoge@mail.com>
Date:   Sat Jan 30 21:17:04 2021 +0900

    third commit

commit abd34263c1667972302f393edd07086c30d9ba47
Author: westhouseK <hogehoge@mail.com>
Date:   Sat Jan 30 21:14:47 2021 +0900

    second commit

commit dcec1b77867265edaf473290caaa786e902ef3a6
Author: westhouseK <hogehoge@mail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit

ここで、以下のコマンドを実行する。(~の数分、コミットのコメントを修正できます。)

$ git rebase -i HEAD~~~
pick dcec1b7 first commit
pick abd3426 second commit
pick ea86c0a third commit

# Rebase dcec1b7..ea86c0a onto dcec1b7 (3 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
# b, break = stop here (continue rebase later with 'git rebase --continue')
# 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

以下のように、修正します。
【やり方】viモードなので、iを押してINSERTモードになり、修正後escを押してから:wqで変更を保存します。

git rebase -i HEAD~~~

pick dcec1b7 first commit
f abd3426 second commit
f ea86c0a third commit

もう一度、gitログを見てる見ると

$ git log
commit 56df6baebebb0bc2017fca3d37a640d6bc7edf8d
Author: westhouseK <hogehoge@gmail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit

これでコミットを一つにまとめることができました👏
これはあくまでも、「second commit」「third commit」のコミットメッセージが削除されただけで、修正内容自体はしっかり反映さえています。ご安心を😌

注意

コミットIDが振り直されて変わるということ

【git rebase前】・・・ ①
commit dcec1b77867265edaf473290caaa786e902ef3a6 ← ここ
Author: westhouseK <hogehoge@gmail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit


【git rebase後】・・・ ②
commit 56df6baebebb0bc2017fca3d37a640d6bc7edf8d ← ここ 
Author: westhouseK <hogehoge@gmail.com>
Date:   Sat Jan 30 20:34:27 2021 +0900

    first commit

つまり、①と②はコメントが同じでもコミット内容は違うという意味です。
なので、 以下は怒られます。

$ git_demo $ git push
To github.com:hogehoge/git_demo.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'git@github.com:hogehoge/git_demo.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

今までのコミットIDと違うものがプッシュされたからです。
なので、git push -fで強制的にプッシュしましょう。

よくわからなくなったら・・・

git rebase --abort

これをすると、rebase前の状態に戻せます。(git rebase -iが成功していると戻せませんが)

終わりに

これで軽微な修正を気軽にできますね。綺麗なブランチを作るように心がけていきましょ!!!

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