33
25

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ノート: コミットを分割

Last updated at Posted at 2018-04-11

ファイルX,Yを一回のコミットで追加したが、これをそれぞれのコミットに分割して追加したことにしたい

$ mkdir X
$ cd X/
$ git init
$ touch README.txt
$ git add README.txt
$ git commit -m "initial commit"
$ touch X Y
$ git add X Y
$ git commit -a -m "add X, Y"

ログ確認

$ git log
commit 6f04e67c167eda51089086644566983c25c9a828
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 14:42:36 2018 +0900

    add X, Y

commit a886aa67c6f2eba22ce19466bf4bec5cf67248f8
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 14:41:45 2018 +0900

    initial commit

最新のコミットを分割し、 X,Yをそれぞれのコミットで追加することにする。

git rebase -i HEAD^1

一行目を編集

-pick 6f04e67 add X, Y
+edit 6f04e67 add X, Y

エディタを終了すると rebaseが一時停止する

Stopped at 6f04e67... add X, Y
You can amend the commit now, with

        git commit --amend

Once you are satisfied with your changes, run

        git rebase --continue

最新のコミットをリセットして

$ git reset HEAD^

X,Yをそれぞれ追加してコミット

$ git add X
$ git commit -a -m "X"
[detached HEAD ed4ff58] X
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 X

$ git add Y
$ git commit -a -m "Y"
[detached HEAD 835430c] Y
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Y

最後に git rebase --continueを実行。

$ git rebase --continue
Successfully rebased and updated refs/heads/master.

ログを確認

$ git log
commit 835430c1e950d5edf6cbcfb15bdaf59c1b34a5db
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 14:47:45 2018 +0900

    Y

commit ed4ff58c43b72148f1e05df78adc5ce6f7e66390
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 14:47:39 2018 +0900

    X

commit a886aa67c6f2eba22ce19466bf4bec5cf67248f8
Author: Nunocky <nunocky@example.com>
Date:   Wed Apr 11 14:41:45 2018 +0900

    initial commit

正直 rebaseしなくても resetだけでいいのではないかと思うのだが、なんだかよく分からない。

33
25
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
33
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?