githubでbranchの状態を過去に戻したいときのあれこれ
pushしたけどイケてないcommitがあったので、消したかった時に起きたカナシミと、その対処法について
かなりテンパったんやで...
こんな感じでミスした
- イケてないcommit以前のcommitのhashを直で指定してcheckoutした
- hashに名前着いてないし、current branchがno branchになる
これを無視した(警告読まないのヤバイ)
Note: checking out 'qw09efj0'.(ハッシュ値) You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example: git checkout -b new_branch_name HEAD is now at qw09efj0... fix breakdown in relations between parents and children
git「今お前no branchだから新しいbranch作れよ」
何でこんなことになるのか
- そもそもbranchやtagはcommitのhashに名前を付けているだけなのだ
というか、そもそも以前のcommitに戻って別branchを派生させるのって良くないよね
pushしたcommitを戻したいならgit revertや!
- 引数に指定したhashのcommitの変更を無かった事にする
- たくさんのcommitに適用したいなら、-nオプションを付けて一回revertする毎にcommitしないようにしよう
Oppai.txt コミットのhash: oaid09w - 胸にゴミが着いていますよ + おっぱい揉ませてください
みたいなイケてないcommitにgit revert oaid09w
すると
Oppai.txt コミットのhash: oaid09w - おっぱい揉ませてください + 胸にゴミが着いていますよ
のように、diffが逆になる(元の状態に戻る)
しかも、ちゃんとOppai,txtのcommitと、それをrevertした事がcommitの歴史に残る!
まだpushしてないcommitを戻したいならgit resetや!
- commitした後にcommit見たら修正したい所があった!!!!
git reset -soft HEAD^
で直前のcommitを取り消してくれる
まとめ
- git revert {hash}を使う
- 指定したhashの変更を+-逆にしてくれる(無かった事にしてくれる)
- しかも、無かった事にした変更が歴史にちゃんと残る
- -nオプションを付けるとrevert毎にcommitしなくなる
- revertし直したら、pushする
- git diff {hash} -statで戻したかった状態のhashとdiffを比較して戻った事を確認する