5
5

More than 3 years have passed since last update.

git rebase -iでgitのコミットをまとめる

Last updated at Posted at 2018-09-02

いけてないログがあるときなどログをまとめたい時

手順

  1. git rebase -i HEAD~
  2. どれをまとめるか変更
  3. (コンフリクトしてたら)コンフリクト解消
  4. PUSHする

git rebase -i でまとめる

$ git rebase -i [commitID]

これで指定したCommitIDの1つ後のところまで表示。
まとめたいコミットの1つ前を指定する。

$ git rebase -i HEAD~~

-i以下の~の数でまとめるログの数
git rebase -i HEAD~2
という書き方でも可能

いい感じにまとめる

# 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

のようなメッセージが出るので
消したいコミットログのpickをfに変更する

p,pick そのコミットを使用
r,reword コミットのメッセージのみ変更。ログは残る
e,edit コミットを修正する
s,suash 1つ前のコミットにまとめる。ログは書き直す
f,fixup 1つ前のコミットにまとめる。ログはその前のに合わせる。

基本は、複数のをまとめたいのでfixupとコメントを編集するeditを多く使用しています。

こんな感じ

  r 71a335aad hoge
  f 6e51e52fb iranai mes
  f 596d7eec7 iranai mes

コンフリクトがあれば解消し、コメントに沿ってaddしてから git rebase --continue を行う

失敗した時はgit rebase --abortを行う

$ git rebase --abort

PUSHを行う

git logなどで変更した後のコミットログを確認してPUSHします。
そのままではpushできないので
ブランチを削除して再度あげるか push -fでpushする

ブランチ消してあげる場合

$ git push origin :<対象ブランチ> // リモートブランチの削除
$ git push

push -fであげる場合

$ git push -f ブランチ

first commitを含む場合はエラーが出るので

git rebase -i --root

でできる

5
5
3

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