12

I just made a new repository on github. Starting with a folder full of files, the steps I did were:

git init
git add -A
git remote add origin ...

#Now pull in the first commit that github made
git pull origin master

#Check everything is OK
ls

Eek! All my files have disappeared! What happened? Can I get them back?

Eric
  • 95,302
  • 53
  • 242
  • 374
  • Did you ever commit and push something? Are the commits on github? – ThiefMaster Apr 22 '12 at 14:51
  • @TheifMaster: No. I figured I should pull in the initial commit from github, then commit my changes on top of that. – Eric Apr 22 '12 at 14:52
  • There is no such thing as an "initial commit". A new repository does not have any commits. – ThiefMaster Apr 22 '12 at 14:52
  • @ThiefMaster: Github now makes one when a repository is created. – Eric Apr 22 '12 at 14:54
  • It seems what happened is a result of nothing having been committed in the local repository. I just confirmed the behavior. Poof, files gone. But, when I redid my test except with a 'git commit' after the 'git add', then the merge happened and the other files were not touched - even files that I had not committed (only added). Ouch. – GoZoner Apr 22 '12 at 17:10
  • 1
    For the record, a pull *never* touches untracked files. It will refuse to proceed, giving you an error along the lines of "operation would overwrite untracked file ". – Cascabel Apr 22 '12 at 19:36
  • it just removed all my files too. – Paul Solt Jan 21 '13 at 03:23

5 Answers5

15

You can get them back. Even though the only thing pointing to it was the index, git add still put the added content in the repo. I'd start with a git fsck to find "dangling" (git's slightly quirky spelling of "unreferenced") blobs and git cat-file -p those blobs, if there's too many I'd do something like find .git/objects -type f | xargs ls -lt.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • 4
    `git fsck --lost-found` is probably the easiest way forward. – CB Bailey Apr 22 '12 at 17:52
  • 5
    As it might not be clear to some people, you do `git cat-file -p ` where the argument is given by the output `git fsck` command. – cdmckay May 12 '12 at 19:39
  • 1
    dangling cat-file fsck those blobs. there's so much funny words in git dam – urmurmur Jun 16 '17 at 07:40
  • could you be a little more specific as to how this is done? I feel like your answer requires understanding of the inner workings of git that I don't have to build up the actual commands you are hinting. – blkpingu May 23 '19 at 11:53
6

For me the following worked:

  1. Go to the affected file.
  2. Right click and choose Local History and select Show History. From there you can see the history of changes made to the file, and then choose the time you want to bring back the file to.
  3. You will get two windows with the left one having >>. Click on all the >> and this will send your changes to the right window.
  4. Close the window and here you go with your file restored to where you wanted it to be.

I hope it helped!

user3518317
  • 61
  • 1
  • 2
5

I agree with the accepted answer, however in my case there were too many results for git fsck. This solution is what helped me locate the lost files:

Search for a string in the missing file(s):

grep -rin <string_in_missing_file> .git/

For example:

grep -rin MyClassName .git/

Search results:

.git//lost-found/other/3cfaa36226f52a5c1b38c2d2da3912656c998826:5:class MyClassName extends ParentClass
.git//lost-found/other/e7b8923de4afb230ad523b5b515f50cb9c624248:5:class MyClassName extends ParentClass

Where search results are:

.git/<path_to_file>:<line_number_of_found_string>:<found_string_and_context>

Then to restore the file:

git cat-file -p 3cfaa36226f52a5c1b38c2d2da3912656c998826 > ../<desired_file_path>/MyClassName.php
Xavier
  • 181
  • 2
  • 4
3

I did the similar thing When I was trying to push my commit and was getting hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.

I tried to revert my commits by

git reset HEAD~3 . 
git stash 
git pull 
git stash pop
git push

and accidentally pulled again and push the changes. All my work for 7 days was lost.

This worked for me to get all of my work back in local branch:

git reset --hard HEAD@{"15 minutes ago"}

qwerty001
  • 31
  • 1
1

Since you never committed the files, no sorry. The steps you need to take are:

git init
git add .
git commit -m 'Initial commit'
git remote add origin ...
git push origin master

Remember, when in doubt, always commit. As long as you do that, you can always undo stuff with git.

rtn
  • 127,556
  • 20
  • 111
  • 121
  • 1
    It's good advice always to commit when in doubt, but following the instructions in the accepted answer will allow you to recover uncommitted files. – Ricky McMaster Nov 09 '17 at 10:26