A long time ago i made some work in a branch, which I deleted during a clean up. So I would like to get it back because the request came up again. I can't remember the exact branch name. I have been looking at how to get git log display name of (deleted) branches, but I can't find it in the logs.
Asked
Active
Viewed 824 times
2
-
even if the branchname is gone, the commits should still be there. Do you remember anything about the changes? If there was a unique string, variable name etc. you can search for in the entire history by using this command. `git log -S somestring` – Kaus2b Oct 11 '21 at 14:59
-
Might be me. But it only seem to search merged branches – Peter Mølgaard Pallesen Oct 12 '21 at 08:27
-
1oh sorry. probably need `--all` with it as well. `git log --all -S somestring` – Kaus2b Oct 12 '21 at 19:11
-
Thanks that works. Restoring the changes with https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git – Peter Mølgaard Pallesen Oct 12 '21 at 20:26
-
Added it as an answer below – Kaus2b Oct 13 '21 at 14:27
2 Answers
3
All you need is this command:
git log --all -S somestring
where somestring is a unique string, variable name etc. that can identify the commits in question.
Once you have the commit sha then you can recreate the branch using:
git checkout -b branch-name commit-sha

Kaus2b
- 747
- 4
- 12
1
I just write a node.js script find-deleted-unmerged-branches to find deleted unmerged branches (may with a name or without a name).
You can have a try with
npx find-deleted-unmerged-branches /path/to/your/git/repo
Will output something like:
┌─────────┬─────────────┬───────────┐
│ (index) │ name │ sha │
├─────────┼─────────────┼───────────┤
│ 0 │ '<unknown>' │ 'bf2ef40' │
│ 1 │ '<unknown>' │ 'dc1a360' │
└─────────┴─────────────┴───────────┘
Then checkout to given commit shas, and check whether it's the deleted branch.

banyudu
- 1,046
- 9
- 19