1

I am trying this git thing and I think I messed up. So I have a project called Foo in my computer. I changed directory into that project. Then did, git init, and then git commit into my github. It said it failed to push some ref to the server, so I googled it, and someone said I needed to do a git pull first. Ok, so I did a git pull request from my github server while I was in the Project Foo's directory. Now all files inside my project Foo directory got removed and only the files from the git pull request are stored inside on my Project Foo's directory.

Help, how do I retrieve my files again?

EDIT: here is the terminal history, http://codepad.org/cg9Gi7Ii - the commands from that log around the point where the files disappeared are:

$ ls
Models      css     js      utils
README      index.html  styles
$ git init
Initialized empty Git repository in /Users/MacBoss/Documents/workspace/BlackboardCanvas/.git/
$ git add .
$ git remote add origin git@github.com:dchhetri/Demo-s.git
$ git push origin master
error: src refspec master does not match any.
error: failed to push some refs to 'git@github.com:dchhetri/Demo-s.git'
$ git remote add origin git@github.com:dchhetri/Demo-s.git
fatal: remote origin already exists.
$ git push origin master
error: src refspec master does not match any.
error: failed to push some refs to 'git@github.com:dchhetri/Demo-s.git'
$ git pull  failed to push some refs to 'git@github.com:dchhetri/Demo-s.git'
fatal: 'failed' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From github.com:dchhetri/Demo-s
 * [new branch]      master     -> origin/master
You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often merge with the same branch, you may want to
use something like the following in your configuration file:

    [branch "master"]
    remote = <nickname>
    merge = <remote-ref>

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.
$ git pull  failed to push some refs to 'git@github.com:dchhetri/Demo-s.git'
fatal: 'failed' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git pull git@github.com:dchhetri/Demo-s.git
From github.com:dchhetri/Demo-s
 * branch            HEAD       -> FETCH_HEAD
$ ls
AStarDemo.jar
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
dchhetri
  • 6,926
  • 4
  • 43
  • 56
  • Besides `git pull`, what other commands did you run? – Chris Cherry Oct 19 '11 at 22:07
  • This is what I did, git init, then touch README, then git commit, then git remote origin git@github.com:dchhetri/Demo-s.git, then git push master, which failed saying some ref did not push to the server, so I did a git pull, and my files were gone. Also I want to add, that previously I did rm -r .git, to remove the .git folder from Foo's project directory, I dunno if that did anything to the files inside Foo. – dchhetri Oct 19 '11 at 22:17
  • I added the terminal history, it shows that I previously before the pull request, all files were there and now after it, it wasn't – dchhetri Oct 19 '11 at 22:47

3 Answers3

3

I've quoted the part of the log of your terminal session that appears to be relevant below. Those are the commands between where your files are present and when they've been replaced by the single AStarDemo.jar. I've formatted so that it's clearer what's going on, and removed commands that failed:

$ ls
Models      css     js      utils
README      index.html  styles

$ git init
Initialized empty Git repository in /Users/[...]/BlackboardCanvas/.git/

$ git add .

$ git remote add origin git@github.com:dchhetri/Demo-s.git

$ git pull git@github.com:dchhetri/Demo-s.git
From github.com:dchhetri/Demo-s
 * branch            HEAD       -> FETCH_HEAD

$ ls
AStarDemo.jar

(Firstly, to explain the situation that you're in, you already have a master branch in your GitHub repository. The most recent version of that branch just has a single file in it, called AStarDemo.jar. The reason that your attempts to push earlier failed is that you're trying to overwrite that master branch with your local branch. You normally can only push a commit to a branch if your commit includes the history of that branch. Unless you "force" the push by adding the -f option, git won't let you do that. At other points in the long terminal history you've linked to, the reason that some of your attempts to push failed is that you're trying git push origin master before you've created any commits - your local master doesn't exist yet.)

In the section of commands that I've quoted, you stage all the files in your directory (with git add .) but don't carry on to commit them. Then you seem to be hit by what strikes me as a bug in git - the command git pull <URL>, when performed in a repository with no commits yet, seems to wipe out your index (the staging area) without warning. The reason that this seems like a bug to me is that if you similarly had staged and uncommitted changes but your HEAD did point to a commit, the git pull <URL> would fail, saying that this would lose your local changes. In this case, it seems to have thrown away your staged files. (I can reproduce this with git version 1.7.5.4.)

As for what to do now - do you have another copy of the files somewhere? If you don't, and you really need to get them back, you may still be able to do so, albeit with a bit of work, so long as you haven't deleted your .git directory since staging those files. (As other people here have commented, in general you should never delete the .git directory, which contains your entire project's history.) You can find some guidance on recovering those staged files in the answer here:

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • Thanks for the information. No I do not have a backup copy. I thought I had lost all hope for now, so I was ready to start all over. If possible, I would want to get the files back. I will try the link you have provided today – dchhetri Oct 20 '11 at 14:27
1

That shouldn't be the cause.

Think of Git as two students on the exam. They know some of the exam answers and once in a while they peek into each-other work looking for recent changes and apply same changes to their own work.

When you "pull" from github, it only copies changes and applies them.

Now what MIGHT have removed your files is "git stash". That hides your changes temporarily and you can see them with "git stash list" or get them back with "git stash pop".

romaninsh
  • 10,606
  • 4
  • 50
  • 70
  • I didn't use git stash command, although I did do rm -r .git, would that have any effect on my Foo's project? It shouldn't. I can't even see the files in my trashbin? – dchhetri Oct 19 '11 at 22:21
  • History is stored inside .git folder. If it's gone and if your files are gone too, then you can't recover them. Perhaps you did `rm -r .` ? – romaninsh Oct 19 '11 at 22:24
  • I checked my terminal history and while in Foo's Directory I did, rm .git -r Was that the problem? – dchhetri Oct 19 '11 at 22:39
  • If your "Foo" directory is empty and you haven't successfully pushed to github, then you can't restore your files through Git. – romaninsh Oct 19 '11 at 22:45
  • I added the terminal history, it shows that I previously before the pull request, all files were there and now after it, it wasn't – dchhetri Oct 19 '11 at 22:47
  • In my edit, I added a linked to the log of my terminal, check line 899. Which was the rm -r .git command. Then line 946, which shows the 'ls' command, which shows all files are still there? – dchhetri Oct 19 '11 at 22:56
0

Hit the following command to undo the pull:

git reset --hard HEAD^
rtn
  • 127,556
  • 20
  • 111
  • 121
  • I get this error when trying to execute it, fatal: ambiguous argument 'HEAD^': unknown revision or path not in the working tree. Use '--' to separate paths from revisions – dchhetri Oct 19 '11 at 22:45
  • I added the terminal history, it shows that I previously before the pull request, all files were there and now after it, it wasn't – dchhetri Oct 19 '11 at 22:47