6
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?

Gitで削除したブランチを復元させる方法

Last updated at Posted at 2025-07-27

はじめに

Gitを使っていて間違ってブランチを削除した時に慌てず復元する方法をメモ代わりにまとめましたので参考にしてみてください。

ブランチの状況

UserName MINGW64 ~/SampleProject (main)
$ git branch
  develop
* main

SamplePrjectという名前のプロジェクトを用意しました。
ブランチはmainとmainをベースに作成したdevelopを用意しています。
developにはコミットを1つ追加している状況です。
イメージ図
image.png

復元を再現する手順

1.developブランチを削除
2.developブランチのコミットのID(SHA1-ID)を確認
3.developブランチの復元

1.developブランチを削除

以下のコマンドでdevelopブランチを削除します。

UserName MINGW64 ~/SampleProject (main)
$ git branch -d develop
warning: deleting branch 'develop' that has been merged to
         'refs/remotes/origin/develop', but not yet merged to HEAD
Deleted branch develop (was 4895a77).

developブランチ削除のイメージ図
image.png
違和感を覚える方もいるかもしれませんが、developブランチを削除してもdevelopブランチに存在していたコミットは一定期間消えません。そのためブランチ削除後も残ったコミットのID(SHA1-ID)さえわかれば復元が可能となります。

コマンドの説明

  1. git branch -d
    ローカルブランチを削除するコマンドです
    -dは「delete(削除)」の略です

  2. develop
    削除したいローカルブランチの名前です。

2.developブランチのコミットのID(SHA1-ID)を確認

developブランチのコミットID(SHA1-ID)に関しては「1.developブランチを削除」での削除コマンドの結果でも確認できますが、以下のようにgit reflogコマンドでも確認が可能です。

UserName MINGW64 ~/SampleProject (main)
$ git reflog
4847efc (HEAD -> main, origin/main) HEAD@{0}: checkout: moving from develop to main
4895a77 (origin/develop) HEAD@{1}: commit: sample commit
4847efc (HEAD -> main, origin/main) HEAD@{2}: checkout: moving from main to develop
4847efc (HEAD -> main, origin/main) HEAD@{3}: checkout: moving from develop to main
4847efc (HEAD -> main, origin/main) HEAD@{4}: checkout: moving from main to develop
4847efc (HEAD -> main, origin/main) HEAD@{5}: commit (initial): initial commit

git reflogとは

reflog(Reference Logの略)=「参照ログ」
自分のリポジトリ内でHEADやブランチがどんな順番で移動したかという履歴を参照できます。(デフォルトの保存期間は90日です。)
今回のログで言うと上から2行目の以下の部分が復元したい対象のコミットとなります。

4895a77 (origin/develop) HEAD@{1}: commit: sample commit

3.developの復元

以下のコマンドでdevelopブランチの復元をします。

UserName MINGW64 ~/SampleProject (main)
$ git checkout -b develop 4895a77
Switched to a new branch 'develop'

コマンドの説明

  1. git checkout -b
    -bオプションは「新しいローカルブランチを作成して、そこに切り替える」コマンドです。

  2. develop
    作成する新しいブランチ名です(復元後のブランチ名)

  3. 4895a77
    復元する対象のコミットID(SHA-1 IDの短縮表記)

仮にリモートブランチを削除した場合でも「3.developの復元」で復元したブランチを以下のコマンドでプッシュすれば問題ありません。

git push -u origin develop

コマンドの説明

  1. git push
    ローカルリポジトリの内容をリモートリポジトリ(origin)に送信(反映)するコマンドです。

  2. origin
    リモートリポジトリの名前(通常originが多い)

  3. develop
    ローカルのdevelopブランチを、リモートにも同じ名前(develop)で作成・更新するという意味です。

  4. -u(--set-upstream)オプション
    「ローカルのdevelopブランチが、リモートoriginのdevelopブランチを追跡(upstream)する」設定も同時に行う。

最後に

今回はGitの削除したブランチの復元方法についてまとめました。
分かりずらいところもあると思いますので、いつでも質問、ご意見お待ちしております。

6
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
6
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?