0

I accidently deleted a local branch I was working on in IntelliJ and now I can't find any history of it.

I've tried using git reflog but theres no history of it on the repo. Im trying to search for it in the git tool > console tab but its not there. Is there any other way I can find/restore this branch I deleted locally?

  • a brach is just a pointer to a commit..... if it's not in reflog it means that you did not _at least_ check it out.... and it might have been created anywhere.... how did you create it in the first place? – eftshift0 Jul 24 '23 at 19:00
  • @eftshift0 I created the new local branch from a remote branch and was making commits to this local branch. It was definitely created as I was working on it for over a week periodically. I stupidly was cleaning up/deleting a bunch of older branches and accidentally deleted this one last week. – Sherbet Head Jul 24 '23 at 19:10
  • 1
    If you were committing on it, then there _must_ be reflog items about _those_ commits and at least a checkout of the branch, if you _did_ check it out.... unless you were working on a different local repo from the one you are checking right now. – eftshift0 Jul 24 '23 at 19:17
  • @eftshift0 Sadly when I run git reflog on my repo theres no history of it – Sherbet Head Jul 24 '23 at 19:38
  • Did you ever your push your branch to GitHub or another remote? – TTT Jul 24 '23 at 20:19
  • 1
    Does this answer your question? [Can I recover a branch after its deletion in Git?](https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git) – grg Jul 24 '23 at 21:06
  • @TTT I never pushed anything, just was committing locally – Sherbet Head Jul 24 '23 at 21:21
  • Unless intellij keeps a log of deleted branches, I'm pretty sure the only tag that is relevant here is `git`. Also, this appears to be a dup of the question referenced by @grg. (And I wish I noticed that comment before I spent the time to write up my answer, which is pretty much the same thing as the second answer to that question.) If an intellij person can confirm we can remove that tag, I think we can remove all tags but "git" and and close this as a dup. – TTT Jul 25 '23 at 05:50

2 Answers2

0

It appears the commit already fell out of your reflog, and unfortunately deleting a branch doesn't create a new reflog entry. But if the orphaned commit still exists, you can brute force finding it:

  1. List all of your orphaned commits. This answer shows you how to create a file called unreachable.txt.
  2. For each of the commits in that file, show the commit ID, date, message title, and author. Perhaps something like (using Git Bash): cat unreachable.txt | grep commit | cut -d " " -f 3 | xargs git log --no-walk --pretty=format:'%h %ad %s | %an' --date=short
  3. Scan through the list of #2 and pick the commit ID you want, and then re-create a branch at that commit: git branch my-new-branch <commit-ID-from-step-2>
TTT
  • 22,611
  • 8
  • 63
  • 69
0

Also, to add an IDE tip:

Once you delete a branch in the IDE, a notification will appear to restore it. If you close it, it will still be preserved in the Notifications tool window.

Dino Letic
  • 71
  • 3