Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
22
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@Nunocky

gitノート: コミットを分割

ファイル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だけでいいのではないかと思うのだが、なんだかよく分からない。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
22
Help us understand the problem. What are the problem?