1

So I added a remote, empty repository to a local repository that was just initialized, and made the mistake of pulling before committing my local changes:

git init
git remote add origin <URL>
git add *
git pull origin master

Now all my files have been removed because git wrote the empty repo into the local one.

Normally, when pulling, git refused to merge if there were unstaged local changes that needed committing. I'm surprised that this is different with the initial commit.

git log shows only the initial commit since I never committed the local changes, so I can't undo the pull using git reset --hard. Is there a way to restore my files and if so, how?

mpe
  • 1,000
  • 1
  • 8
  • 25
  • does a `gitk ORIG_HEAD` show you the last state? – eckes Feb 04 '13 at 15:41
  • 1
    possible duplicate of [Undo a git pull](http://stackoverflow.com/questions/4719205/undo-a-git-pull) – CharlesB Feb 04 '13 at 15:43
  • @eckes: no, it shows only the initial commit. – mpe Feb 04 '13 at 15:51
  • @CharlesB: the question is the same, however the accepted answer in that other thread don't work in my situation (and has a typo). – mpe Feb 04 '13 at 16:00
  • @mpe do you refer to this answer? http://stackoverflow.com/a/1109433/ Should show a hidden commit that has your staged files – CharlesB Feb 04 '13 at 16:02
  • @CharlesB: Yes, I mean that answer. The provided command does not do what it advertises. The one in the accepted answer to this question does. – mpe Feb 04 '13 at 16:07

2 Answers2

5

Judging by the work-flow you gave above, it appears that you staged your files. That should leave some dangling blobs out there, that you should be able to recover.

git fsck --lost-found

That should commit the changes to a file in .git/lost-found. Something might show up there.

Further discussion: Recover file after git pull, Recovering added file after doing git reset --hard HEAD^

Community
  • 1
  • 1
  • This gives me some files that contain the original content. I can reconstruct my project from them. Thanks! – mpe Feb 04 '13 at 16:02
-1

If the changes you lost were not committed, git isn't aware of them and cannot recover them. Always commit or stash uncommited changes before taking actions that may modify the state of the working directory.

Jonathan Patt
  • 1,572
  • 14
  • 13
  • While I really appreciate your warning, your answer is not correct (see accepted answer). Thanks anyway! – mpe Feb 04 '13 at 16:05
  • Ah, sorry, I hadn't realized that you'd staged your changes. When you do so, git, of course, becomes aware of them. – Jonathan Patt Feb 04 '13 at 16:18