0
0

More than 1 year has passed since last update.

git rebase について

Last updated at Posted at 2022-02-21

目次

1.前回
2.概要
3.内容
4.用語のまとめ
5.感想
6.おわりに

1. 前回

よく使うgitコマンド

2. 概要

git rebaseでよく使うオプション

3. 内容

git rebase は
コミット履歴が分かりづらかったり、2つ以上のコミットを1個に統合したいときに使うコマンド

git rebaseはbrunch上で行うコマンドで
masterやmainでは個人開発などではない限り使わないほうがいい。

前回のgit記事を一部引用

# リポジトリ初期化(ローカルリポジトリの作成)
$ mkdir rebase_test && cd rebase_test && git init
$ touch README.md && git add .
$ git commit -m init

# GitHub上にリポジトリを作成、リモートにもpushする。
# リモートリポジトリが作成されweb上に展開される(public or private)
$ git remote add origin https://github.com/<あなたのGitHubアカウント名>/sample_app.git
$ git push -u origin master

#ブランチを作成 static-pagesはブランチ名
$ git checkout -b static-pages

#ブランチの切り替わりを確認
$ git branch

#ブランチへ変更点があったものをGitリポジトリへ追加
$ git add -A
$ git commit -m "変更点を記載"
$ git push -u origin static-pages

#コミット内容を追記
$ echo 'あいうえお' >> README.md
$ git add . && git commit -m 'あいうえおと入力'
$ echo 'かきくけこ' >> README.md
$ git add . && git commit -m 'かきくけこを入力'
$ echo 'さしすせそ' >> README.md
$ git add . && git commit -m 'かきくけこを入力'
$ echo '12345' >> README.md
$ git add . && git commit -m 'test'
$ echo 'test' >> README.md
$ git add . && git commit -m 'test'
$ echo 'test' >> README.md
$ git add . && git commit -m 'test'
$ echo 'test' >> README.md
$ git add . && git commit -m 'test'
$ echo 'test' >> README.md
$ git add . && git commit -m '12345を入力'
$ echo '67890' >> README.md
$ git add . && git commit -m '678999を入力'
$ echo 12345 >> README.md
$ echo 67890 >> README.md
$ git add . && git commit -m '数値をやり直し'

# $ git log でログを確認。
# $ git log --reverse --onelineとやると上から下へログを追うことができる。

$ git log --reverse --oneline
c49731e (master) init
34e6137 あいうえおと入力
206942c かきくけこを入力
2d0987d かきくけこを入力
8171c7d test
15d5996 test
206eeaf test
9b81a9e test
ab60d64 12345を入力
6092d9b 678999を入力
ec06e8b (HEAD -> static-pages) 数値をやり直し

出力先のREADME.mdは以下の状態となる。

rebase_test/README.md
あいうえお
かきくけこ
さしすせそ
1 2 3 4 5
test
test
test
test
6 7 8 9 0
67890
12345

git rebase -i masterでコミット履歴やコミット分を編集する前に
masterかmainかをgit branchで確認。

git branch      
  master
* static-pages

作成brunchで指定されて、masterであることを確認
git rebase -i masterを実行すると以下のコミット名が表示される。
主に使うのはs, squashとe, editである。
※間違えて開いてしまった場合、そのままwqやq!で閉じてしまうと
保存されてしまうので、全てdで削除してからq!でviから抜ける。

git rebase -i master

pick 34e6137 あいうえおと入力
pick 206942c かきくけこを入力
pick 2d0987d かきくけこを入力
pick 8171c7d test
pick 15d5996 test
pick 206eeaf test
pick 9b81a9e test
pick ab60d64 12345を入力
pick 6092d9b 678999を入力
pick ec06e8b 数値をやり直し
pick f248ff9 数字やり直し

# Rebase c49731e..f248ff9 onto c49731e (11 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.
#

s,eで省略できるため、修正したいコミットを指定して
pickからsやeを選択する。
s:コミットをまとめたいときに使い、
sの上へコミットがまとめられる仕様です。
e:コミット名のみを編集するときに使うものです。

pick 34e6137 あいうえおと入力
pick 206942c かきくけこを入力
e 2d0987d かきくけこを入力   
pick 8171c7d test
s 15d5996 test
s 206eeaf test
s 9b81a9e test
pick ab60d64 12345を入力
e 6092d9b 678999を入力   
pick ec06e8b 数値をやり直し
pick f248ff9 数字やり直し

viを抜けた後に以下のような「かきくけこを入力」というコミットをどのように編集するか聞かれています。
これはgit commit --amendでコミット名を編集して
その後、git rebase --continueを実行して編集を反映するよう説明がされております。

その際、対象のコミット履歴がさかのぼりますので
コード内も前の状態に戻りますが、git rebase --continueを実行すれば
最新の状態に戻りますので落ち着いて対応してください。
※筆者はgit rebase --continueを忘れて少し困惑しました。

git rebase -i master

Stopped at 2d0987d...  かきくけこを入力
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

「さしすせそを入力」へ変更した後、wqで抜け
git rebase --continueで反映させた後
次の編集情報が表示されます。


かきくけこを入力 ←「さしすせそを入力」へ変更

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Feb 21 09:11:07 2022 +0900
#
# interactive rebase in progress; onto c49731e
# Last commands done (3 commands done):
#    pick 206942c かきくけこを入力
#    edit 2d0987d かきくけこを入力
# Next commands to do (8 remaining commands):
#    pick 8171c7d test
#    squash 15d5996 test
# You are currently editing a commit while rebasing branch 'static-pages' on 'c49731e'.
#
# Changes to be committed:
#       modified:   README.md
#

これは次の編集状態でtestと4つ連続で並んでいるので
s, squashによってコミットをまとめます。

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

test

# This is the commit message #2:

test

# This is the commit message #3:

test

# This is the commit message #4:

test

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Feb 21 09:11:20 2022 +0900
#
# interactive rebase in progress; onto c49731e
# Last commands done (7 commands done):
#    squash 206eeaf test
#    squash 9b81a9e test
# Next commands to do (4 remaining commands):
#    pick ab60d64 12345を入力
#    edit 6092d9b 678999を入力
# You are currently rebasing branch 'static-pages' on 'c49731e'.
#
# Changes to be committed:
#       modified:   README.md
#

4つの「test」というコミットを
「testまとめ」とコミット名を変更、wqで抜ける。


testをまとめ

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Feb 21 09:11:20 2022 +0900

次の処理でamendを求められているため、
「678999を入力」からを「6789を入力」へ変更

PC@name rebase_test % git rebase --continue
[detached HEAD ff04c36] testをまとめ
 Date: Mon Feb 21 09:11:20 2022 +0900
 1 file changed, 4 insertions(+)
Stopped at 6092d9b...  678999を入力
You can amend the commit now, with

  git commit --amend 

Once you are satisfied with your changes, run

  git rebase --continue

git commit --amendとすると編集画面が表示される。

678999を入力

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Feb 21 09:12:22 2022 +0900
#
# interactive rebase in progress; onto c49731e
# Last commands done (9 commands done):
#    pick ab60d64 12345を入力
#    edit 6092d9b 678999を入力
# Next commands to do (2 remaining commands):
#    pick ec06e8b 数値をやり直し
#    pick f248ff9 数字やり直し
# You are currently editing a commit while rebasing branch 'static-pages' on 'c49731e'.
#
# Changes to be committed:
#       modified:   README.md
#

その後忘れずにgit rebase --continueで反映させる。
残りの数字やり直しというコミット名も同じように編集すればコミット名は全て編集完了。

pick ec06e8b 数値をやり直し
pick f248ff9 数字やり直し

全ての編集が完了していることを
git log --reverse --onelineで確認。

git log --reverse --oneline
c49731e (master) init
34e6137 あいうえおと入力
206942c かきくけこを入力
21c07ec さしすせそを入力
ff04c36 testをまとめ
fdb8cc3 12345を入力
7b57f94 6789を入力
9f8926f (HEAD -> static-pages) 数字やり直し

問題なさそうであればgit push -fとmergeを行う。
※個人開発であればいいが、チームで行う場合はPRは必須。

mergeとpush
$ git checkout master
$ git merge static-pages
$ git push

ローカルリポジトリのブランチが不要であれば削除

ブランチを削除
$ git branch -d static-pages

以上が、git rebase によるコミット結合、コミット名編集でした。
git rebaseは簡潔に言うとこれらが使えます。

コミット履歴がわかりやすくなる
コミットメッセージを後から変える
2つ以上のコミットを1個に統合する
一度コミットした内容を編集する

4. 用語のまとめ

用語 意味
e, edit コミット名を編集
s, squash コミットをまとめる

5. 感想

よくわからない状態で自分の環境、ローカルリポジトリでsquashを行い
ある程度の動作は確認できたものの、editdでgit rebase --continueの処理を忘れて冷や汗をかきgit reflogでコミット履歴を戻し事なきを得る。
このreflogについては次回にまとめようかと思います。

6. おわりに

まだまだ知識は浅いので、git rebaseについては都度編集や更新をかけていくかと思います。

0
0
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
0
0