Help us understand the problem. What is going on with this article?

最初のcommitから最後までのcommit履歴をまとめる

More than 1 year has passed since last update.

最初のcommitから最後までのcommit履歴をまとめる

環境

Git 2.9.5

概要

下記のコマンドを使用し、最初のcommitから最後までのcommit履歴をまとめる
解説をします。

command
git rebase -i --root

詳細

gitを使っていると、commit履歴を細かく分けすぎてしまい
まとめたくなることがあります。

以下は、test用のファイルのcommitを細かく分けすぎて
しまった場合です。

git log コマンドを使用しcommit履歴を見てみます。

command
git log
コマンドプロンプトの表示
commit 38ee2821d7a35a76e5df0d16f47e93825dd2b293
Author: sfk0105 <test@example.com>
Date:   Wed Aug 23 14:48:07 2017 +0900

    [add] test3を追加

commit fa54d389630ff5cc64ee0e534eccdccfa1d35d1f
Author: sfk0105 <test@example.com>
Date:   Wed Aug 23 14:47:32 2017 +0900

    [add] test2を追加

commit 5a0549d2ed67750cfa7dd455e1ce50f0c5242ea3
Author: sfk0105 <test@example.com>
Date:   Wed Aug 23 14:46:55 2017 +0900

    [add] test1を追加


今回は最初のcommit(test1)から最後のcommit(test3)までを、まとめたいと思います。
以下のコマンドを使いまとめます。

command
git rebase -i --root

実行すると以下の画面が表示されます。

コマンドプロンプトの表示
pick 93244e6 [add] test1を追加
pick 805ad0d [add] test2を追加
pick 416bf58 [add] test3を追加

# Rebase 416bf58 onto c108753 (3 command(s))
#
# 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
# d, drop = remove commit
#
# 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


今回はtest1からtest3まで、まとめたいので以下の変更を加ます。
変更箇所としてはtest2とtest3のpickをsquashにしただけです。

コマンドプロンプトの表示
pick 93244e6 [add] test1を追加
squash 805ad0d [add] test2を追加
squash 416bf58 [add] test3を追加

# Rebase 416bf58 onto 34c8149 (3 command(s))
#
# 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
# d, drop = remove commit
#
# 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


保存し終了すると以下の画面が表示されます。

コマンドプロンプトの表示
# This is a combination of 3 commits.
# The first commit's message is:
[add] test1を追加

# This is the 2nd commit message:

[add] test2を追加

# This is the 3rd commit message:

[add] test3を追加

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Wed Aug 23 14:46:55 2017 +0900
#
# interactive rebase in progress; onto 34c8149
# Last commands done (3 commands done):
#    squash 805ad0d [add] test2を追加
#    squash 416bf58 [add] test3を追加
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on '34c8149'.
#
#
# Initial commit
#
# Changes to be committed:
#       new file:   test1
#       new file:   test2
#       new file:   test3
#

保存して終了して、logを見てみます。

command
git log
コマンドプロントの表示
commit a2045ad29c0c4a28a1c8a159916fc4342c81e49c
Author: sfk0105 <test@example.com>
Date:   Wed Aug 23 14:46:55 2017 +0900

    [add] test1を追加

    [add] test2を追加

    [add] test3を追加

commitがまとめられていることがわかります。

以上です。

捕捉

補足として、rebase -i コマンドを使用した時に出る下記のコメントの意味を
表にしておきます。

コマンドプロンプトの表示
# 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
# d, drop = remove commit

コマンド 説明
pick 特になにもされない
reword コメントのみ修正
edit 修正のため一時停止
squash 1つ前のコミットとまとめられる
fixup 1つ前のコミットとまとめられる(まとめられるコミットのコメントは消える)
exec シェルを使ってコマンド実行
drop コミットが、消える
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away