Trouble with git! I accidentally removed my project files?

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

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:

  • Undo git reset --hard with uncommitted files in the staging area

  • 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".


    点击以下命令撤消拉:

    git reset --hard HEAD^
    
    链接地址: http://www.djcxy.com/p/4024.html

    上一篇: 恢复由Git结帐移除的文件

    下一篇: 麻烦与混帐! 我不小心删除了我的项目文件?