3
3

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 -i あなたの履歴を守ります

Last updated at Posted at 2019-04-01

c.txt と d.txt を一緒にcommitしちゃってる!
分けたい!分けたい!(大事なことなので2回言いました)

❯ git log
commit 3320bf4a4c8d7165e30282cdcd6795602d00bdc3 (HEAD -> master)
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:06:54 2019 +0900

    add b.txt & c.txt

commit d5f820d0fd366702fa388ebad947b76f9c7ed1b1
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:06:32 2019 +0900

    add b.txt

commit 540edae6ace436c9c1817afd5d549fc422a5d34d
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:06:25 2019 +0900

    add a.txt



私は、git rebase -i あなたの履歴を守ります (HEAD~1で直近のコミット履歴を編集します)
❯ git rebase -i HEAD~1

エディタ上で先頭のpickをeditに変えて保存します

before


pick bbaedb1 add c.txt & d.txt

# Rebase 76a09a7..bbaedb1 onto 76a09a7 (1 command)
# 〜略〜

after


edit bbaedb1 add c.txt & d.txt

# Rebase 76a09a7..bbaedb1 onto 76a09a7 (1 command)
# 〜略〜

1つ前のコミットに戻します(zshなので^を¥でエスケープしています)
❯ git reset HEAD\^

Untrackedになっています
❯ git status
interactive rebase in progress; onto d5f820d
Last command done (1 command done):
   edit 3320bf4 add b.txt & c.txt
No commands remaining.
You are currently editing a commit while rebasing branch 'master' on 'd5f820d'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	c.txt
	d.txt

nothing added to commit but untracked files present (use "git add" to track)


1つずつコミットします (c.txt)
❯ git add c.txt
❯ git commit c.txt -m "add c.txt"

1つずつコミットします (d.txt)
❯ git add d.txt
❯ git commit d.txt -m "add d.txt"

git rebase -i を終了します
❯ git rebase --continue
Successfully rebased and updated refs/heads/master.

❯ git log
commit 0478f9e80bc4f4a11683ef9e1271f2dda0f56cbf (HEAD -> master)
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:27:43 2019 +0900

    d
commit 3320bf4a4c8d7165e30282cdcd6795602d00bdc3 (HEAD -> master)
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:06:54 2019 +0900

    add b.txt & c.txt

commit d5f820d0fd366702fa388ebad947b76f9c7ed1b1
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:06:32 2019 +0900

    add b.txt

commit 540edae6ace436c9c1817afd5d549fc422a5d34d
Author: yamazoon <xxxx@gmail.com>
Date:   Tue Apr 2 02:06:25 2019 +0900

    add a.txt



reflogすると、履歴の編集を(start)と(finish)の間に書いてくれています
❯ git reflog
21d9934 (HEAD -> master) HEAD@{0}: rebase -i (finish): returning to refs/heads/master
21d9934 (HEAD -> master) HEAD@{1}: commit: add d.txt
3a322e7 HEAD@{2}: commit: add c.txt
d5f820d HEAD@{3}: reset: moving to HEAD^
3320bf4 HEAD@{4}: rebase -i: fast-forward
d5f820d HEAD@{5}: rebase -i (start): checkout HEAD~1
3320bf4 HEAD@{6}: commit: add b.txt & c.txt
d5f820d HEAD@{7}: commit: add b.txt
540edae HEAD@{8}: commit (initial): add a.txt

疲れたので一旦おしまい


補足:もちろん、git logも 「add c.txt」 と 「add d.txt」で分かれてコミットされています

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?