LoginSignup
18
15

More than 3 years have passed since last update.

git rebaseで複数のコミットを1つにまとめる

Last updated at Posted at 2019-09-11

最近の勉強で学んだ事を、ノート代わりにまとめていきます。
主に自分の学習の流れを振り返りで残す形なので色々、省いてます。
Webエンジニアの諸先輩方からアドバイスやご指摘を頂けたらありがたいです!

複数のコミットを1つにしたい

一度commitし、プルリクエストを出したブランチに対してレビュー修正が入り再度コミットをしたのですが
レビュアーから「rebaseしてコミットを1つにまとめてね〜」と言われ、git rebaseしようとした時にまとめです。

git rebase -iをする

今回の場合は1プルリクエストにつき1コミットにしたい場合で、複数のコミットを1つにまとめてくれるgit rebase -iコマンドを使いました。
-iとは-interactiveの略です。

まずはgitのログを以下のコマンドで確認します。

$git log --oneline

例えば、最新のコミット2つを1つのコミットにまとめたい場合は以下のコマンドを入力します。

git rebase -i HEAD~2 

すると、こんな画面になります

pick af42728 create navigation
pick dc0944f create Landing Page

# Rebase 2505c1e..dc0944f onto 2505c1e (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
# 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.
#
"~/Desktop/Nuno Theme Starter Files/.git/rebase-merge/git-rebase-todo" 28L, 1226C

知らないコマンドがいっぱい出てきて、どうしたら良いのか分からなくなりました。

git rebase -i には
複数のコミットをやり直す
コミットを並び替える、削除する
コミットをまとめる
コミットを分割する

といった事もできるので今回はコミットをまとめる為のコマンド
・p, pick: コミットをそのまま残す
・s, squash: コミットを前のコミットに統合する

今回はcreate Landing Pageのコミットをcreate navigationのコミットに統合したいのでcreate Landingをpickからs(squash)に編集します。

pick af42728 create navigation
s dc0944f create Landing Page

# Rebase 2505c1e..dc0944f onto 2505c1e (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
# 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.
#
"~/Desktop/Nuno Theme Starter Files/.git/rebase-merge/git-rebase-todo" 28L, 1226C

あとは、:wqで保存してファイルを抜けました。すると以下のような画面になり

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

create navigation

# This is the commit message #2:

create Landing Page

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Sep 11 19:13:09 2019 +0700
#
# interactive rebase in progress; onto 2505c1e
# Last commands done (2 commands done):
#    pick af42728 refs task #1 create navigation
#    squash dc0944f refs task #1 create Landing Page
# No commands remaining.
# You are currently rebasing branch 'feature/task1' on '2505c1e'.
#
# Changes to be committed:
#       modified:   Nuno Theme Starter Files/index.html
#
~
"~/Desktop/Nuno Theme Starter Files/.git/COMMIT_EDITMSG" 24L, 717C

git logを確認するとコミットがcreate navigationだけになり1つにまとめられていました!

$ git log --oneline
6332db2 (HEAD -> feature/task1) create navigation
2505c1e (origin/master, origin/develop, master, develop) first commit

あとはこれをリモートリポジトリに反映したいのですがgit pushをするとこのようなエラーが発生しました。

 ! [rejected]        feature/task1 -> feature/task1 (non-fast-forward)
error: failed to push some refs to 'git@github.com:hoge/hoge.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.

おそらくこれは前のコミットと競合してしまう為、前のコミットを上書きする形で強制的にpushを行います。このときに使う-f オプションは -forceを意味します。これはリモートレポジトリを強制的に変更してしまうため、使う際には注意が必要です。

git push -f

無事に複数のコミットをまとめることができました!!

18
15
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
18
15