git

gitのメモです

750 views

コミットした後やっぱり間違えた、直して再びコミットすると、コミットの履歴が増産され、ソースの差分も訳が分からなくなる。
そんなときは git rebase -iを使う。
以下、動作確認。

まず、適当なフォルダでgitを初期化する。

git init

そのあと、a.txtを作成してコミットする。

echo aaa > a.txt
git add .
git commit -m "first commit"

同様に2回コミットする。

echo bbb > a.txt
git add .
git commit -m "second commit"
echo ccc > a.txt
git add .
git commit -m "third commit"

git logでコミット履歴を見る。

D:\tmp\gittest>git log
commit 23357cab95630ef71a9571e87a5fb0cd3847f05c
Author: konishi <konickcioc@gmail.com>
Date:   Thu Mar 19 10:14:51 2020 +0900

    third commit

commit 5b7c105ffb0860eb9d2a522d3a3c6e2d55b8b315
Author: konishi <konickcioc@gmail.com>
Date:   Thu Mar 19 10:14:34 2020 +0900

    second commit

commit 32b8f3e0c67d96859815be25e6ae30696d1faa8b
Author: konishi <konickcioc@gmail.com>
Date:   Thu Mar 19 10:14:09 2020 +0900

    first commit

3回コミットされていることが分かる。
これを一つのまとめたい。
HEADから二つ分をまとめたいので、以下を実行する。

git rebase -i HEAD~2

すると、vimで以下の画面が表示される。

pick 5b7c105 second commit
pick 23357ca third commit

# Rebase 32b8f3e..23357ca onto 32b8f3e (2 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

コマンドのsの説明に「use commit, but meld into previous commit」と書いてあり、日本語に直すと「コミットを使用しますが、前のコミットに結合します」である。
まさにこれがやりたいので、sに変更する。
<before>

pick 5b7c105 second commit
pick 23357ca third commit

<after>

pick 5b7c105 second commit
s 23357ca third commit

すると、コミットメッセージの変更画面が表示される。

# This is a combination of 2 commits.
# The first commit's message is:

second commit

# This is the 2nd commit message:

third commit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Mar 19 10:37:52 2020 +0900
#
# interactive rebase in progress; onto edafc37
# Last commands done (2 commands done):
#    pick 2cbb0f4 second commit
#    s e669024 third commit
# No commands remaining.
# You are currently editing a commit during a rebase.
#
# Changes to be committed:
#       modified:   a.txt

コミットメッセージを変更する。

# This is a combination of 2 commits.
# The first commit's message is:

naoshimasita.

# This is the 2nd commit message:

second wo merge shimasita.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Thu Mar 19 10:37:52 2020 +0900
#
# interactive rebase in progress; onto edafc37
# Last commands done (2 commands done):
#    pick 2cbb0f4 second commit
#    s e669024 third commit
# No commands remaining.
# You are currently editing a commit during a rebase.
#
# Changes to be committed:
#       modified:   a.txt

保存すると、マージされている。

commit 1c9dc00ea40cd8b955e7bb7efee3b29ff90676ed
Author: konishi <konickcioc@gmail.com>
Date:   Thu Mar 19 10:37:52 2020 +0900

    naoshimasita.

    second wo merge shimasita.

commit edafc37e1146e5d4295f7751086526679bed86ef
Author: konishi <konickcioc@gmail.com>
Date:   Thu Mar 19 10:32:34 2020 +0900

    first commit

Page 1 of 1.



[添付ファイル]


お問い合わせ

プロフィール

マッスル

自己紹介

本サイトの作成者。
趣味:プログラム/水耕栽培/仮想通貨/激辛好き
プログラムは趣味と勉強を兼ねて、のんびり本サイトを作っています。
フレームワークはdjango。
仮想通貨はNEMが好き。
水耕栽培は激辛好きが高じて、キャロライナ・リーパーの栽培にチャレンジ中。

サイト/ブログ

https://www.osumoi-stdio.com/pyarticle/

ツイッター

@darkimpact0626