Eric
Eric

Reputation: 97631

Git pull deleted uncommitted changes

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?

Upvotes: 13

Views: 19944

Answers (6)

Sebastian W
Sebastian W

Reputation: 11

Another tip if you are using Intellij is right clicking a folder and go to Local History and revert to any state there. Found this solution after googling after having this issue so dropping here if helpful for anyone else.

Upvotes: 1

qwerty001
qwerty001

Reputation: 31

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"}

Upvotes: 3

user3518317
user3518317

Reputation: 61

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!

Upvotes: 6

Xavier
Xavier

Reputation: 181

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

Upvotes: 5

jthill
jthill

Reputation: 60423

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.

Upvotes: 15

ralphtheninja
ralphtheninja

Reputation: 133118

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.

Upvotes: 1

Related Questions