4

I have a huge problem. Yesterday I mistaken development and production terminals opened side-by-side and ran

git add .

on production. That caused staging all files in public/uploads path. When I tried

git status

It showed all files in public/uploads are staged and ready to commit. But I never committed these changes, because on production I don't want any commits or pushes. SSH key on production doesn't have permission to push, only clone/pull.

So I ran these commands to force pull my new code from remote repository

git fetch --all
git reset --hard  origin/master
git pull origin master

But now I find out that it has deleted all files in public/uploads path and the directory it self. When I check

git status

I see "Your branch is up-to-date with 'origin/master' ". Is there a way how to recover files from deleted directory? These files are pretty important...

Leon
  • 31,443
  • 4
  • 72
  • 97
Patrik Šimunič
  • 449
  • 1
  • 4
  • 16
  • All you had to do was to read carefully the output of `git status`. It shows how to unstage a file before the list of staged files: *"use `"git reset HEAD ..."` to unstage"* – axiac Jan 19 '17 at 11:36
  • Is it enough for you to get the contents of the files? This is easy to restore, but you have to provide the respective file names yourself – Vampire Jan 19 '17 at 11:57
  • Possible duplicate of [Recover files that were added to the index but then removed by a git reset](http://stackoverflow.com/questions/10782978/recover-files-that-were-added-to-the-index-but-then-removed-by-a-git-reset) – Andreas Wederbrand Jan 19 '17 at 12:01
  • Might be relevant: https://stackoverflow.com/questions/1108853/recovering-added-file-after-doing-git-reset-hard-head – Sven Koschnicke Jan 19 '17 at 12:04
  • 2
    Possible duplicate of [Recovering added file after doing git reset --hard HEAD^](http://stackoverflow.com/questions/1108853/recovering-added-file-after-doing-git-reset-hard-head) – Leon Jan 19 '17 at 12:10

1 Answers1

2

As far as I understand, git add registers its argument files in the repository even before they are committed. If they are not committed, then they will be garbage collected later. Therefore there should be a way of restoring the files from the repository if you act promptly.

Try running the following bash script in the root of your repository directory. It will create a new subdirectory recovering_lost_files that will contain git objects that are newer than your HEAD commit (and therefore correspond to git add-ed but uncommitted files). Unfortunately, the names of the files will not be automatically recovered.

recover_lost_files:

#!/usr/bin/env bash

headcommit="$(git log --format=format:%H)"
headcommitobject=".git/objects/${headcommit:0:2}/${headcommit:2}"
mkdir recovering_lost_files
find .git/objects/ -type f -newer "$headcommitobject"|while read -r path
do
    obj="${path#.git/objects/}"
    obj="${obj/\/}"
    git cat-file -p $obj > recovering_lost_files/$obj
done
Leon
  • 31,443
  • 4
  • 72
  • 97