vscodeでsquashしてコミットをまとめる

2020年12月21日

vscodeでsquashしてコミットをまとめる

vscodeでコミットをまとめる方法を書いておきたいと思います。
コマンドで言うと「rebase -i」となるみたいですね。

参考:gitのコミットの歴史を改変する(git rebase) 1 / 2

squashしてコミット履歴をまとめる

下記のようなコミット履歴があるとします。
「ta」「ti」「tu」「teto」をまとめて一つのコミットにしたいと思います。

1.まとめたいコミットの一つ手前のコミット履歴を右クリックする。
(今回の場合は「sasisusesoを追記しました。」を右クリック)
2.「Rebase current branch on this Commit」をクリックする。
3.「Launch Interactive Rebase in new Terminal」にチェックを付ける。
4.「Yes, rebase」をクリックする。

エディタに下記のようなコミット履歴が表示されます。
(選択されたコミットより新しいコミット履歴)


pick 0954904 taを追記しました。
pick 897d82d tiを追記しました。
pick 83e35e7 tuを追記しました。
pick 25c814b tetoを追記しました。
pick c9bd4a8 naninunenoを追記しました。

# Rebase 3774b9f..c9bd4a8 onto 3774b9f
#
# 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
#
# 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.
#
"/home/.git/rebase-merge/git-rebase-todo" 23L, 816C

「ta」を追記したコミット履歴に「ti」「tu」「teto」を集約したいので、「ti」「tu」「teto」の履歴の「pick」を「squash」または「s」に書き換えます。書き換え後は下記の通りです。


pick 0954904 taを追記しました。
s 897d82d tiを追記しました。   
s 83e35e7 tuを追記しました。   
s 25c814b tetoを追記しました。   
pick c9bd4a8 naninunenoを追記しました。

「:wp」で保存して閉じると下記の通りブランチが作成できているのが確認できます。またエディタが立ち上がり修正を求められます。

下記の通りに修正します。
【修正前】


# This is a combination of 4 commits.
# The first commit's message is:
taを追記しました。# --> tatitutetoを追記しました。

# This is the 2nd commit message:

tiを追記しました。# --> [削除]

# This is the 3rd commit message:

tuを追記しました。# --> [削除]

# This is the 4th commit message:

tetoを追記しました。# --> [削除]

【修正後】


# This is a combination of 4 commits.
# The first commit's message is:
tatitutetoを追記しました。

# This is the 2nd commit message:



# This is the 3rd commit message:



# This is the 4th commit message:



# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached from 0954904
# You are currently editing a commit while rebasing branch 'master' on '3774b9f'.
#
# Changes to be committed:

「:wq」で保存して閉じると下記の通りコミット履歴が集約されているのが確認できました。

まとめ

次からはリモートリポジトリもこねくり回したいと思います。

YouTube

2020年12月21日