2
0

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.

大事なブランチをローカルからもリモートからも消したけど元に戻せた

2
Posted at

ブランチを消したときの対処法

現状

悲惨なことに大事なブランチをremoteからもlocalからも消し去ってしまいました

手順

$ git reflog

まずはこちらのコマンドを叩く↑
するとコミット履歴が出てくる↓

96f72ec7 (HEAD -> develop, origin/develop, origin/HEAD) HEAD@{0}: checkout: moving from deleted-branch to develop
5480e0d9 HEAD@{1}: commit: adjust
cab12ab2 HEAD@{2}: commit: response data debug
9b7873cf HEAD@{3}: commit: debug log modify
c506e1f5 HEAD@{4}: commit: apiUrl = null
b1c620b4 HEAD@{5}: commit: debug comment
e7df592a HEAD@{6}: commit: debug comment
8e3473d3 HEAD@{7}: commit: comment out
....

ちょうど一番上のところに大事なブランチを消し去った痕跡が残っていますね。
消したブランチで作業してたときの最新の状態 に戻したいので
上から二行目の

5480e0d9 HEAD@{1}: commit: adjust

このコミットをしたときの状態に戻したいです。

そこでこのコマンドを叩く↓

$ git branch new-branch HEAD@{1}

分かるかと思いますが少し解説・・・・

$ git branch つけたいブランチ名 HEAD@{戻したいコミットのHEAD番号}

そしてこのままgit push!!
(intelliJならCtrl + Shift + kでgit push できます)

そうすると先ほどのコマンドを叩くときに指定したブランチ名で復活します。
めでたしめでたし・・・・・

備忘録

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?