【GitHub】リベース:複数のコミットを1つにまとめる方法!

この記事からわかること

  • Git複数ブランチ1つまとめる方法
  • git rebaseコマンド使い方
  • ローカルではなくリモートプッシュ済みの場合の解決方法

index

[open]

\ アプリをリリースしました /

みんなの誕生日

友達や家族の誕生日をメモ!通知も届く-みんなの誕生日-

posted withアプリーチ

リベース:rebaseとは

Gitのリベース(rebase)とは、ブランチ履歴を整理するためのコマンドです。リベースの使い所は複数のコミットに切り分けたが、コミット履歴が煩雑になってしまい、整理したい場合に複数のコミットを1つのコミットにまとめたい時です。

使い方

リベースするためにはまずgit log --onelineコミット履歴を確認しておきます。

$ git log --oneline
2a2b159 (HEAD -> develop) コメント3
33e347e コメント2
8100f18 コメント1

この「コメント3」と「コメント2」のコミットをまとめて1つにするにはrebaseコマンドに-iオプションで HEAD~2(HEADから2つ目まで)を指定して実行します。すると以下のような画面になります。

$ git rebase -i HEAD~2

pick 33e347e コメント2
pick 2a2b159 コメント3

# Rebase 8100f18..2a2b159 onto 8100f18 (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 [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case
#                    keep only this commit's message; -c is same as -C but
#                    opens the editor
# 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

$ git push -f origin master

Vimで操作できるのでiでインサートモードに切り替えてpickとなっているところをsquashに変更し:wqでファイルを閉じます。

pick 33e347e コメント2
squash 2a2b159 コメント3

続いてコメントを編集することができるので希望のコメントに変更して:wqでファイルを閉じます。#で始まる部分はコメントには含まれないので不要なコメントを削除するだけとかでも大丈夫です。

# This is a combination of 2 commits.
# This is the 1st commit message:

コメント2

# This is the commit message #2:

コメント3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Fri Jan 19 19:50:22 2024 +0900
#
# interactive rebase in progress; onto 8100f18
# Last commands done (2 commands done):
#    pick 33e347e コメント2
#    squash 2a2b159 コメント3
# No commands remaining.
# You are currently rebasing branch 'develop' on '8100f18'.
#
# Changes to be committed:

これでリベースは完了です。履歴を確認してみると以前の2つのコミットはたらしいコミットに変更されていることがわかります。

$ git log --oneline
c60310f (HEAD -> develop) コメント2
8100f18 コメント1

リモートにプッシュしていた場合

リモートにすでにコミットをプッシュしていたがリベートしたい場合は、先ほどの手順でリベース後にgit push -fで強制プッシュすることで解決することができます。

$ git push -f origin master
$ git push --force origin master

まだまだ勉強中ですので間違っている点や至らぬ点がありましたら教えていただけると助かります。

ご覧いただきありがとうございました。

searchbox

スポンサー

ProFile

ame

趣味:読書,プログラミング学習,サイト制作,ブログ

IT嫌いを克服するためにITパスを取得しようと勉強してからサイト制作が趣味に変わりました笑
今はCMSを使わずこのサイトを完全自作でサイト運営中〜

New Article

index