7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

git rebaseでコミットを並べ替える

Posted at

コミットの順番をあとから並べ替える実験。「ファイルA,B,Cを追加したが、A,C,Bの順でコミットしてしまった。 A,B,Cの順にしてコミットとファイルの整合性をとりたい」というシナリオ。

mkdir X
cd X
git init
touch A
git add A
git commit -m "A"
touch C
git add C
git commit -m "C"
touch B
git add B
git commit -m "B"

logを確認する。

$ git log -l HEAD~2
commit 8fde7ad6837d3fd69eff0284cdf57d95ae58ba16
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 13:38:56 2018 +0900

    B

commit 73b9b66d4adf3e56ea2a45a5b4be0086cfd4be88
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 13:38:48 2018 +0900

    C

commit a89a9a0d4ba0c70d500a6da48eb872d04284a40b
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 13:38:39 2018 +0900

    A

この履歴を A,B,Cの順に並べ替えたい。そのために git rebaseを用いる。

git rebase -i HEAD~2

pick 73b9b66 C
pick 8fde7ad B

# Rebase a89a9a0..8fde7ad onto a89a9a0
#
# 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の行を書き換える。B,Cを入れ替えてエディタを終了する。

pick 8fde7ad B
pick 73b9b66 C

もう一度ログを確認

$ git log -l HEAD~2
commit 9c0c8762922977b0ea5996b27b3845433e9cdd3e
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 13:38:48 2018 +0900

    C

commit 2f583be0b740fc5e02a8e54a3489bbe0fca9ad99
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 13:38:56 2018 +0900

    B

commit a89a9a0d4ba0c70d500a6da48eb872d04284a40b
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 13:38:39 2018 +0900

    A

good

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?