git reflogを使ってコミットを確認して戻す
こんなGitのコミットがあるとします。
$ git log --oneline
03cf5a4 (HEAD -> master) commit 10
84d8c6c commit 9
46bf42c commit 8
3d8d4f2 commit 7
cc1a120 commit 6
512dcb6 commit 5
a3b1556 commit 4
4cce44d commit 3
b3c25fc commit 2
dcc7245 commit 1
f40706d test commit
ここでcommit 7
までのコミットをまとめたいとします。
rebase
でちょっと多めのコミットを見て
$ git rebase -i HEAD~5
pick cc1a120 commit 6
pick 3d8d4f2 commit 7
pick 46bf42c commit 8
pick 84d8c6c commit 9
pick 03cf5a4 commit 10
...
とかしてcommit 10
とかをsquash
に変更してまとめます。
$ git log --oneline
733e933 (HEAD -> master) commit 7, commit 8, commit 9, commit 10
cc1a120 commit 6
512dcb6 commit 5
a3b1556 commit 4
4cce44d commit 3
b3c25fc commit 2
dcc7245 commit 1
f40706d test commit
ここで、commit 7
はまとめるべきではなかった、とかやっぱり別のコミットにした方がわかりやすい、
と考え直したとします。
git log
を見ても以前の元のコミットは残ってませんが、
git reflog
で各コミットの状態を見ることが出来ます。
$ git reflog
733e933 (HEAD -> master) [email protected]{0}: rebase -i (finish): returning to refs/heads/master
733e933 (HEAD -> master) [email protected]{1}: rebase -i (squash): commit 7, commit 8, commit 9, commit 10
335514b [email protected]{2}: rebase -i (squash): # This is a combination of 3 commits.
09986a1 [email protected]{3}: rebase -i (squash): # This is a combination of 2 commits.
3d8d4f2 [email protected]{4}: rebase -i (start): checkout HEAD~5
03cf5a4 [email protected]{5}: rebase -i (finish): returning to refs/heads/master
03cf5a4 [email protected]{6}: rebase -i: fast-forward
84d8c6c [email protected]{7}: rebase -i: fast-forward
46bf42c [email protected]{8}: rebase -i: fast-forward
3d8d4f2 [email protected]{9}: rebase -i (start): checkout HEAD~3
03cf5a4 [email protected]{10}: commit: commit 10
84d8c6c [email protected]{11}: commit: commit 9
46bf42c [email protected]{12}: commit: commit 8
3d8d4f2 [email protected]{13}: commit: commit 7
cc1a120 [email protected]{16}: commit: commit 6
512dcb6 [email protected]{17}: commit: commit 5
a3b1556 [email protected]{18}: commit: commit 4
4cce44d [email protected]{19}: commit: commit 3
b3c25fc [email protected]{20}: commit: commit 2
dcc7245 [email protected]{21}: commit: commit 1
f40706d [email protected]{22}: commit (initial): test commit
こんな感じで操作履歴が見れます。
ここでcommit 10
の状態まで一度戻せばまた好きなようにコミットをまとめられるので、
$ git reset --hard 03cf5a4
HEAD is now at 03cf5a4 commit 10
これで
$ git log --oneline
03cf5a4 (HEAD -> master) commit 10
84d8c6c commit 9
46bf42c commit 8
3d8d4f2 commit 7
cc1a120 commit 6
512dcb6 commit 5
a3b1556 commit 4
4cce44d commit 3
b3c25fc commit 2
dcc7245 commit 1
f40706d test commit
元に戻りました。
ここからまたgit rebase -i
とかで好きな様にまとめればOK。
reflogも見てみると
$ git reflog
03cf5a4 (HEAD -> master) [email protected]{0}: reset: moving to 03cf5a4
733e933 [email protected]{1}: rebase -i (finish): returning to refs/heads/master
733e933 [email protected]{2}: rebase -i (squash): commit 7, commit 8, commit 9, commit 10
335514b [email protected]{3}: rebase -i (squash): # This is a combination of 3 commits.
09986a1 [email protected]{4}: rebase -i (squash): # This is a combination of 2 commits.
3d8d4f2 [email protected]{5}: rebase -i (start): checkout HEAD~5
03cf5a4 (HEAD -> master) [email protected]{6}: rebase -i (finish): returning to refs/heads/master
03cf5a4 (HEAD -> master) [email protected]{7}: rebase -i: fast-forward
84d8c6c [email protected]{8}: rebase -i: fast-forward
46bf42c [email protected]{9}: rebase -i: fast-forward
3d8d4f2 [email protected]{10}: rebase -i (start): checkout HEAD~3
03cf5a4 (HEAD -> master) [email protected]{11}: commit: commit 10
84d8c6c [email protected]{12}: commit: commit 9
46bf42c [email protected]{13}: commit: commit 8
3d8d4f2 [email protected]{14}: commit: commit 7
cc1a120 [email protected]{15}: rebase -i (finish): returning to refs/heads/master
cc1a120 [email protected]{16}: rebase -i (start): checkout HEAD~3
cc1a120 [email protected]{17}: commit: commit 6
512dcb6 [email protected]{18}: commit: commit 5
a3b1556 [email protected]{19}: commit: commit 4
4cce44d [email protected]{20}: commit: commit 3
b3c25fc [email protected]{21}: commit: commit 2
dcc7245 [email protected]{22}: commit: commit 1
f40706d [email protected]{23}: commit (initial): test commit
こんな感じになっていて、commit 10
の03cf5a4
に戻っていますが、
先程まとめた状態の733e933
も残っていることがわかります。
やっぱりさっきので良いや、となったら同じ様にreset
を使って戻ることが出来ます。
Sponsored Links