P456678
P456678

Reputation: 185

Restore a local branch that was deleted week ago - Git

I deleted work on a local branch a month ago. I don't remember what the branch name was called.

Is there a way to show the names of all the local branches I've had The git reflog this doesnt seem to be of any use, it only shows about 20 commits and nothing about them being on a local branch or its name.

Upvotes: 0

Views: 93

Answers (1)

CodeWizard
CodeWizard

Reputation: 142612

Is there a way to show the names of all the local branches

To see the current local branches:

git branch 

if the local branch was not merged or deleted?

If you have not merged the local to any other branch of if you deleted it and git reflog is not helpful to you, you can always use the git fsck --lost-found to print out lists of dangling commits (commits which are not accessible from any other commit/branch).

git fsck --lost-found

--lost-found

Write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/, depending on type.

If the object is a blob, the contents are written into the file, rather than its object name.

Once you have those commits you can simply print them out using: git show <SHA-1> and once you see a tree search for the root commit and you can now restore it.


# find out all dangling (loos) objects 
git fsck --lost-found

# find out the desired root tree using the git cat-file or git show
 
# Search for tree objects
git cat-file -t <SHA-1>

#  once a tree was found print its content
git cat-file -p <SHA-1>

# OR

# again print it content in different way
git show <SHA-1>

# once you found your lost tree - recover it
git checkout <branch_name> <SHA-1>

Some more advanced options:

git fsck --full --no-reflogs --unreachable --lost-found

# --full       = Checkout all object in other locations 
                 (read doc to find all about it)

# --no-reflogs = This option is meant only to search for commits that used
                 to be in a ref, but now aren’t, but are still in that 
                 corresponding reflog.

# --unreachable= Print out objects that exist but that aren’t reachable from
                 any of the reference nodes.

Upvotes: 1

Related Questions