0

I was about to backup a new project remotely with git for the first time in a while.

Setup git, did an initial commit of the code, was about to push when I realised there was a resource file with data I'd rather not push. Went googling for how to undo the initial commit and stupidly just followed a post without fully understanding results (I know, but it's late, it's been a long day).

Did a git update-ref -d HEAD followed by git reset --hard.

I may have done a git add . again afterwards, not sure. Now ls and the IDE are showing everything as deleted.

git status is showing Changes to be committed: followed by what looks like all of my source, in green, with each item marked as new file

Below that is Changes not staged for commit: along with all the files listed again,in red, marked as deleted

I'm hoping the source is still somewhere under git and there's a way of retrieving it? From the git status it looks like simply committing it will do it and I can push that? But I don't want to take any chances.

(in case it's relevant for other means of recovering the files, Ubuntu 18.04, data is on an ntfs formatted drive)

Mick O'Hea
  • 1,619
  • 2
  • 14
  • 20
  • 2
    If you committed the files previously the commit likely still exists. Take a look at https://stackoverflow.com/questions/10099258/how-can-i-recover-a-lost-commit-in-git – Ben W Apr 09 '20 at 00:45
  • 1
    The `git update-ref -d` step may have deleted the reflog. In that case, to augment Ben W's suggestion, run `git fsck` and look at commits whose hash ID this prints as "dangling commit". – torek Apr 09 '20 at 04:46
  • Thanks for the suggestions. I'd no previous commits, reflog showed nothing. Nor did git fsck. I think I've managed to recover most of the source using TestDisk, still checking – Mick O'Hea Apr 09 '20 at 13:36

2 Answers2

0

In case it helps anyone else, I tried the suggestions in the comments (thanks for those).

I'd no previous commits, so reflog showed nothing, and there were no dangling commmits with git fsck. They might work for someone else under different circumstances though.

I took a copy of the folder and tried committing the staged files and pushing them, which worked but I'd failed to notice they were only the compiled target files, not the source, so that didn't help.

In the end, I was lucky enough to recover everything using TestDisk to restore the deleted files.

Lessons learnt - always start committing and making remote backups from the start, even if it's just a pet project; and being tired is no excuse for breaking the rule of not running random commands from t'internet without fully understanding the potential consequences

Mick O'Hea
  • 1,619
  • 2
  • 14
  • 20
0

reflog, and a lot of other things, need HEAD to work. But git update-ref -d HEAD deleted HEAD.

$ git update-ref -d HEAD
$ git log HEAD
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

$ git reflog
fatal: your current branch 'master' does not have any commits yet

But the reflog is there!

$ cat .git/refs/HEAD
0000000000000000000000000000000000000000 511454e5c9b2226dc8f7f20d386037c16373bac1 Michael G. Schwern <schwern@pobox.com> 1586460927 -0700   commit (initial): first
511454e5c9b2226dc8f7f20d386037c16373bac1 0000000000000000000000000000000000000000 Michael G. Schwern <schwern@pobox.com> 1586460936 -0700
0000000000000000000000000000000000000000 8bd6cfbcdc36f55a6f681445eb8530f56238136d Michael G. Schwern <schwern@pobox.com> 1586460969 -0700   commit (initial): new first
8bd6cfbcdc36f55a6f681445eb8530f56238136d 511454e5c9b2226dc8f7f20d386037c16373bac1 Michael G. Schwern <schwern@pobox.com> 1586461084 -0700   reset: moving to 511454e

Make another commit so you have HEAD. Then the reflog will work.

...commit anything...

$ git reflog
8bd6cfb (HEAD -> master) HEAD@{0}: commit (initial): new first
511454e HEAD@{2}: commit (initial): first

In this example git reset --hard 511454e would recover the first commit.


git update-ref -d HEAD; git reset --hard is overkill for revising an existing commit.

Instead, edit and add as normal. Then use git commit --amend to rewrite the last commit. See Rewriting History in Pro Git for more.

Schwern
  • 153,029
  • 25
  • 195
  • 336