閉じる
閉じる

あまめも

「Git」の履歴が見づらいのでまとめたい、そんな時の修正方法

まとめ

こんにちは! あますた(@amaguristar)です。

  • 「Gitのコミット同じ様なのがあるので1つにまとめたい」
  • 「Gitのログが見づらいから整理したい」

そんな時ありませんか? 簡単に整理できますよ。

 

「rebase -i 」を使う

  1. rebase -iで修正箇所の範囲を絞る
  2. vi を使いまとめる場所を指定
  3. 合わせた時のメッセージを記載

の3工程。

以下、git log --onelineで下記サンプルデータが出力された場合を想定。

6a764b1 (HEAD -> master, origin/master, origin/HEAD) add-em-tag
81471bc add-charset
1f56a01 modified-ctk-well-caption
b675528 modified-ctk-well-caption
6e4c233 add-ctk-well
bb7e8fb add-index-comments
5f59404 add-balloon-border
b2cc960 add-balloon
f12ce56 first-commit

1. rebaseで修正箇所の範囲を絞る

方法は2パターン。

  • 1-1. git rebase -i HEAD~番号
  • 1-2. git rebase -i コミット識別子
1-1. git rebase -i HEAD~番号

自分を1と数えて修正したい番号を入力。上記サンプルのデータで3番目と4番目のデータであるmodified-ctk〜略〜を1つにまとめたい場合は、

git rebase -i HEAD~4

と入力。

1-2. git rebase -i コミット識別子

修正したいコミット識別子の1つ先の識別子を入力。「1-1」と同じデータを修正したい場合は、

git rebase -i 6e4c233

と入力。

2. vi を使いまとめる場所を指定

「工程1」で入力が成功するとviエディタが下記の様に開きます。

pick b675528 modified-ctk-well-caption
pick 1f56a01 modified-ctk-well-caption
pick 81471bc add-charset
pick 6a764b1 add-em-tag

# Rebase 6e4c233..6a764b1 onto 6a764b1 (4 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

ここで、まとめたい場所の一番左の「pick」を「squash(もしくはs)」に変更して保存。viエディタを終了。

viエディタの編集方法は割愛。今度機会があれば書きます。
3. 合わせた時のメッセージを記載

「工程2」で「pick」を「squash(もしくはs)」に変更したら、下記内容がviエディタで表示されるので、

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

modified-ctk-well-caption

# This is the commit message #2:

modified-ctk-well-caption

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Oct 5 01:12:09 2019 +0900
#
# interactive rebase in progress; onto 6e4c233
# Last commands done (2 commands done):
#    pick b675528 modified-ctk-well-caption
#    squash 1f56a01 modified-ctk-well-caption
# Next commands to do (2 remaining commands):
#    pick 81471bc add-charset
#    pick 6a764b1 add-em-tag
# You are currently rebasing branch 'master' on '6e4c233'.
#
# Changes to be committed:
#       modified:   ctk-template.css
#

まとめた後のコメントを下記の様に修正して保存。

modified-well-caption

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Oct 5 01:12:09 2019 +0900
#
# interactive rebase in progress; onto 6e4c233
# Last commands done (2 commands done):

~~以下略~~

git log --onelineを使い確認すると、

be419f9 (HEAD -> master) add-em-tag
746f54e add-charset
0299b87 modified-well-caption
6e4c233 add-ctk-well
bb7e8fb add-index-comments
5f59404 add-balloon-border
b2cc960 add-balloon
f12ce56 first-commit

修正されているのが確認できます。

まとめ

  1. rebase -iで修正箇所の範囲を絞る
  2. vi を使いまとめる場所を指定
  3. 合わせた時のメッセージを記載

の3工程。

関連記事

  1. [甘]ブログの見出しのデザインが気になったから少し調べてみた。

  2. [甘]パンくずリストつけてみた。

  3. list

    定義リスト<dl><dt><dd&…

  4. WordPressで<div>の様なHTMLタグを表…

  5. ブログデータをローカルに移す方法を良く忘れるのでメモしとこ〜っ

  6. take092-thumbnail

    【WaznFilm更新】MarkdownをWordPressのG…

こんなメモもありますよ♪

  1. 検索
  2. 頂いたお酒

Twitter

ページ上部へ戻る