Skip to contentSkip to conversation

How to restore the original head after `git-reset --hard master^`

DISCUSSION Ben Lau 4 participants 5 messages Jun 15, 2016

lore
Share & tools
lore

Ben Lau

author
#0 lore raw

Hi all,

After involves the command git-reset --hard master^ in a
repository, the working
tree , current head and index are restored to the parent of master. That includes the content of '.git/refs/head/master'. Although the original commit object is still existed , I couldn't find it back.

Is there anyway to recover the original master branch ? or I have to
restore the branch
manually by finding out all the child of current head? If that is the solution , what command could provide this information?

Thanks

#1 lore raw
Quoted 7 lines · Ben Lau

After involves the command git-reset --hard master^ in a
repository, the working
tree , current head and index are restored to the parent of master. That includes
the content of '.git/refs/head/master'. Although the original commit object is still existed
, I couldn't find it back.

I think you can find it in .git/ORIG_HEAD.

#2 lore raw
In reply to Ben Lau
Quoted 7 lines · Ben Lau

After involves the command git-reset --hard master^ in a repository,
the working tree , current head and index are restored to the parent of master. That includes the content of '.git/refs/head/master'. Although the original commit object is still existed , I couldn't find it back.

Is there anyway to recover the original master branch ?

Just run "git-fsck-objects" and look at any dangling commits. The commit and related objects are still there (unless you've run "git prune"), so once you find the name of it, you're all done.

First check that you have the right one by doing

git log <name-you-found>

and if that looks right, you can just do

	git reset --hard <name-you-found>

and you should be back in business.

Linus

#3 lore raw
Quoted 7 lines · Linus Torvalds
Quoted 6 lines · Ben Lau

After involves the command git-reset --hard master^ in a repository,
the working tree , current head and index are restored to the parent of master. That includes the content of '.git/refs/head/master'. Although the original commit object is still existed , I couldn't find it back.

Quoted 5 lines
Quoted 1 line

Is there anyway to recover the original master branch ?

Just run "git-fsck-objects" and look at any dangling commits. The commit and related objects are still there (unless you've run "git prune"), so once you find the name of it, you're all done.

That reminds me: if you lose all of your refs, it's hard to get them back, because git-fsck-objects aborts if you don't specify any heads or have any refs. Is there some reason for this check?

-Daniel
*This .sig left intentionally blank*

#4 lore raw
Quoted 4 lines · Daniel Barkalow

That reminds me: if you lose all of your refs, it's hard to get them back, because git-fsck-objects aborts if you don't specify any heads or have any refs. Is there some reason for this check?

"git prune" is the reason for that check. Pruning your whole object store is likely not what you want ;)

You might make it abort only when "--unreachable" has been specified..

Linus