LoginSignup
26
29

More than 5 years have passed since last update.

git rebaseと仲良くなろう~part2

Posted at

rebaseのinteractiveモードってなんぞや

2個以前の前のコミットメッセージを編集したり、
コミットをまとめたりできることです。

例を見てみましょう

コミットメッセージを編集する

コミットログを見て、

$ git log --oneline

68be895 commit D
71c87eb commit E
8ed1455 commit C
73c200d commit B
9110548 commit A

「commit B」以降のコミットを編集したい時は、1つ前のコミットIDを指定する
git rebase -i {コミットID}

$ git rebase -i 73c200d

pick 8ed1455 commit C
pick 71c87eb commit E
pick 68be895 commit D

# Rebase 73c200d..68be895 onto 73c200d
#
# 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.
#
# Note that empty commits are commented out

コミットメッセージを編集したいものをpick→rに変更

pick 8ed1455 commit C
r 71c87eb commit E
pick 68be895 commit D

(省略)

コミットメッセージ編集画面が表示されるので、編集する

commit E(編集済)

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached from 8ed1455
# You are currently editing a commit while rebasing branch 'develop' on '73c200d'.
#
# Changes to be committed:
#   (use "git reset HEAD^1 <file>..." to unstage)
#
#   modified:   file1.txt
#

コミットメッセージが編集されたか確認する

$ git log --oneline

b455028 commit D
2b5e516 commit E(編集済)
8ed1455 commit C
73c200d commit B
9110548 commit A

新たなコミットメッセージを指定して、2つのコミットを合体させるsquash

コミットログを確認する

$ git log --oneline

b455028 commit D
2b5e516 commit E(編集済)
8ed1455 commit C
73c200d commit B
9110548 commit A

commit Bと commit Cを合体させたいので、
commit AのコミットIDでrebase コマンドを実行する

$ git rebase -i 9110548

squashは1個前のコミットと合体するので、commit Cのところを
pick→s に編集し、保存する

pick 73c200d commit B
s 8ed1455 commit C
pick 2b5e516 commit E(編集済)
pick b455028 commit D

# Rebase 9110548..b455028 onto 9110548
#
# 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.
#
# Note that empty commits are commented out

コミットメッセージ編集画面が表示されるので、編集する

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

commit B+C

(省略)

確認する

$ git log --oneline

242e918 commit D
a2352de commit E(編集済)
5f39c4a commit B+C
9110548 commit A

コミットメッセージは変えずに、コミットを合体させるfixup

コミットログを確認する

$ git log --oneline
242e918 commit D
a2352de commit E(編集済)
5f39c4a commit B+C
9110548 commit A

commit E(編集済)とcommit B+C 合体させたいので
commit E(編集済)のpick→f 変更して、保存する

$ git rebase -i 9110548

pick 5f39c4a commit B+C
f a2352de commit E(編集済)
pick 242e918 commit D

ログを確認する

$ git log --oneline
38a91a4 commit D
0f35ea5 commit B+C
9110548 commit A

commit E(編集済)ログが消えて
コミットメッセージは、「commit B+C」が残っていることが分かる。
もちろん、コミットIDは5f39c4aから0f35ea5に変わっている。

26
29
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
26
29