DRYな備忘録

Don't Repeat Yourself.

まちがえてcommit --amendしちゃった

手元でmodify3して、 素直にcommitして

- commit3
- [origni/master] commit2
- commit1
- commit0

っていう状態にして、pushしたかったのに、手元が狂ってcommit –amendしちゃったとき。

reflog使おう

reflogが

% git reflog
5123b8c HEAD@{0}: commit (amend): commit2
86daca5 HEAD@{1}: commit: commit2

となってるはずなので

% git reset HEAD@{1}

とすれば、–amendしちゃったmodify3をmodifiedな状態でsoft resetできる

reflog使いたくない(なんで?)

commit1までsoft reset

% git reset commit1

modifiedになったmodify2とmodify3(== amendされちゃったふたつのcommit)をstash

% git stash

今HEADがcommit1(remoteとconflictのない状態)なので、remoteから綺麗なcommit2を引っ張り直すことができる

% git pull origin master

これで、以下の状態に復帰できた

- [origin/master] commit2
- commit1
# そして手元にはmodify2+modify3のstashがある

f:id:otiai10:20150711182828j:plain

手元の modify2+modify3なstashを、このブランチにぶっこむ

% git stash pop
# "commit2"と"modify2+modify3(stash)"の間にCONFLICTがあるかもね

CONFLICTを解消すれば、modify3をサルベージできたことになるので、add & commitするなりすればよい(–amendすんなよ)

雑感

  • reflogつかおう? 便利だよ

DRY