Recent

Author Topic: GIT Hard Reset  (Read 2205 times)

Aruna

  • Hero Member
  • *****
  • Posts: 807
GIT Hard Reset
« on: August 26, 2024, 03:37:17 pm »
It has been a while since I last used git. Last night I hard reset a repo only to later find all my projects were gone. Nuked. Is there anyway to recover from this? I forgot my own cardinal rule: Before you do anything make a backup. HELP! 

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
Re: GIT Hard Reset
« Reply #1 on: August 26, 2024, 04:46:16 pm »
In short

 git reflog

... more coming....
« Last Edit: August 26, 2024, 04:48:39 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
Re: GIT Hard Reset
« Reply #2 on: August 26, 2024, 04:55:20 pm »
In long

You can (most likely) get back anything that you had committed.

Uncommitted changes from your worktree are gone.

Code: Text  [Select][+][-]
  1. git reflog -n 20  main
Or of course instead of "main" whatever branch you did reset.

You get a list of all the previous "top commits" of that branch. I don't know if the reset will have added just one or maybe more entries to the reflog. Find the commit sha1 before the reset.

Then you can do
  git log <sha>
  git branch newbranchname <sha>

and that gets you a branch at your lost commit. You can just try different sha, until you found the one you look for.


A reflog normally (if you did not disable) exists for each branch.

If you delete a branch, then the reflog goes. If that branch was recently checked out, then "git reflog HEAD" may still help

You can configure the age kept in the reflog (afaik 6 weeks by default)



Once you have the right commit, you can "hard reset" the original branch to it.

Make sure to stash all changes before, and to reset the correct branch.
« Last Edit: August 26, 2024, 05:13:25 pm by Martin_fr »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
Re: GIT Hard Reset
« Reply #3 on: August 26, 2024, 05:02:51 pm »
If you don't have a reflog then the commit will likely still be there (if you haven't done a "git gc" or similar with a "prune" date of "NOW").

By default any commits, that aren't in a branch any more are still there for 2 weeks (then they get removed).

Code: Text  [Select][+][-]
  1. git fsck
will show you all the loose commits.
Code: Text  [Select][+][-]
  1. dangling commit e3b17ea937b34a17b8d3d5e4a75e72e08ea4f8ef
  2.  

However it wont tell you what they are. You just get the sha. You then have to check each sha with "git log". And there can be a great many. So that will be hard work.

You probably want to put them in a text file, then process them automate to run "git log -n1 --oneline <sha>" for each and grep for something in the commit message. (maybe use a log format with date but on a single line)

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
Re: GIT Hard Reset
« Reply #4 on: August 26, 2024, 05:09:18 pm »
Mind you: Still do a backup. If your .git folder gets damages => then that is it....

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: GIT Hard Reset
« Reply #5 on: August 26, 2024, 06:42:59 pm »
Uncommitted changes from your worktree are gone.
Ughhh... then I am royally screwed. I did a
Code: Text  [Select][+][-]
  1. git init
then
Code: Bash  [Select][+][-]
  1. git add .
then
Code: Bash  [Select][+][-]
  1. git status
and it showed me two untracked folders (lazarus and the sound repo from fred_Vs) both of which I needed but never thought longer about this I went and did a git
Code: Pascal  [Select][+][-]
  1. reset --hard
and sadly I never committed. So all my stuff is gone.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: GIT Hard Reset
« Reply #6 on: August 26, 2024, 06:48:19 pm »
Mind you: Still do a backup. If your .git folder gets damages => then that is it....
You know I have issues with this @Martin_fr that was my effin code that I wrote into the midnight hours. And git has no business removing my original local repo without asking me first? I should have made backups, I didn't. So have to re-write everything from scratch. Which is ok but I do not think I will ever use git again. imho a git --reset hard should revert the repo back to what it originally was instead of cleaning up the folder as in all your code is now POOF! ( I am such an idiot :( )

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: GIT Hard Reset
« Reply #7 on: August 26, 2024, 06:52:27 pm »
@Martin_fr thank you very much for taking the time and explaining these things to me. Very much appreciate your time. (I want to break git to teeny weeny yellow polka dot bikini pieces and jesting I am not, anyone else care to join me?)  The repo was sentimental to me, of no value to anyone else. Just my learning experiences with Lazarus. I was going to put them all together as in a demo. Ah well... lesson learnt!

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
Re: GIT Hard Reset
« Reply #8 on: August 26, 2024, 07:00:47 pm »
Ok in theory... (I haven't tested).

"git add" would have put the files into the repo. (each file would have an sha / and each directory maybe... not sure if git add would have created directories already, maybe not).

But it won't have created a commit.

I am not sure, git fsck may show the "dangling blob" or similar, and then "git show <sha>" would tell about each what it is. But it would take forever to analyse every dangling blob. (and I am not even sure they will be there).

Also, that would be the state each file had at the time of "git add", any edits thereafter would not be saved. So if you had just created the files, then maybe (with plenty of luck) you could recover those near empty files....

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: GIT Hard Reset
« Reply #9 on: August 26, 2024, 07:28:57 pm »
So if you had just created the files, then maybe (with plenty of luck) you could recover those near empty files....
Naah, that is way too much time better spent re-writing the damn thing again. I am wondering does it say anywhere in git's documentation that  --reset hard will erase your world? That is a rather serious design flaw me thinks? Given I screwed up but still a warning about what is about to happen would have been nice? I can't do a git fsck coz I was so upset I went and deleted the .git ( I know... I know.. )

wp

  • Hero Member
  • *****
  • Posts: 13566
Re: GIT Hard Reset
« Reply #10 on: August 27, 2024, 12:23:31 am »
Still being a git novice... But shouldn't the reset work on the local repository only? So if there is a remote repository there should be the possibility to restore the last pushed state by doing a pull.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12426
  • Debugger - SynEdit - and more
    • wiki
Re: GIT Hard Reset
« Reply #11 on: August 27, 2024, 12:26:12 am »
yes, if he had pushed. But he hadn't even committed.

Also not every git has a remote, and if it has some branches still may not have.

Aruna

  • Hero Member
  • *****
  • Posts: 807
Re: GIT Hard Reset
« Reply #12 on: August 27, 2024, 04:40:50 am »
Still being a git novice... But shouldn't the reset work on the local repository only? So if there is a remote repository there should be the possibility to restore the last pushed state by doing a pull.
Yes @wp you are 110% corrrect  the thing is I had just created the remote repo and had not pushed anything up there yet. I was trying to do this when I noticed the untracked branches and I went and unleashed git --reset hard which I sadly found out had quite literally manged to erase my world. ( I still want to break (g)it to tiny pieces)

 

TinyPortal © 2005-2018