git 有问题!我不小心删除了我的项目文件?

发布于 2024-12-10 14:42:21 字数 3270 浏览 0 评论 0原文

我正在尝试这个 git 的事情,但我想我搞砸了。所以我的电脑中有一个名为 Foo 的项目。我将目录更改为该项目。然后,git init,然后 git commit 到我的 github 中。它说它无法将一些引用推送到服务器,所以我用谷歌搜索它,有人说我需要先做一个 git pull 。好的,当我位于 Foo 项目的目录中时,我从 github 服务器发出了 git pull 请求。现在,我的项目 Foo 目录中的所有文件都被删除,只有 git pull 请求中的文件存储在我的项目 Foo 目录中。

求助,如何再次检索我的文件?

编辑:这是终端历史记录, http://codepad.org/cg9Gi7Ii - 来自该日志的命令文件消失的点是:

$ 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 [email protected]:dchhetri/Demo-s.git
$ git push origin master
error: src refspec master does not match any.
error: failed to push some refs to '[email protected]:dchhetri/Demo-s.git'
$ git remote add origin [email protected]: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 '[email protected]:dchhetri/Demo-s.git'
$ git pull  failed to push some refs to '[email protected]: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 '[email protected]:dchhetri/Demo-s.git'
fatal: 'failed' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git pull [email protected]:dchhetri/Demo-s.git
From github.com:dchhetri/Demo-s
 * branch            HEAD       -> FETCH_HEAD
$ ls
AStarDemo.jar

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 [email protected]:dchhetri/Demo-s.git
$ git push origin master
error: src refspec master does not match any.
error: failed to push some refs to '[email protected]:dchhetri/Demo-s.git'
$ git remote add origin [email protected]: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 '[email protected]:dchhetri/Demo-s.git'
$ git pull  failed to push some refs to '[email protected]: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 '[email protected]:dchhetri/Demo-s.git'
fatal: 'failed' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
$ git pull [email protected]:dchhetri/Demo-s.git
From github.com:dchhetri/Demo-s
 * branch            HEAD       -> FETCH_HEAD
$ ls
AStarDemo.jar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

留蓝 2024-12-17 14:42:21

我引用了您的终端会话日志中似乎与下面相关的部分。这些是文件所在位置和文件被单个 AStarDemo.jar 替换之间的命令。我已经进行了格式化,以便更清楚地了解发生了什么,并删除了失败的命令:(

$ 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 [email protected]:dchhetri/Demo-s.git

$ git pull [email protected]:dchhetri/Demo-s.git
From github.com:dchhetri/Demo-s
 * branch            HEAD       -> FETCH_HEAD

$ ls
AStarDemo.jar

首先,为了解释您所处的情况,您的 GitHub 存储库中已经有一个 master 分支。该分支的最新版本只有一个文件,名为 AStarDemo.jar。您之前尝试推送失败的原因是您试图覆盖该 master。 code> 分支与您当地的分支。通常,如果您的提交包含该分支的历史记录,则只能将提交推送到该分支。除非您通过添加 -f 选项“强制”推送,否则 git 不会让您这样做。您链接到的长期终端历史记录中的其他点,您的一些尝试推送失败的原因是您在创建任何提交之前尝试 git push origin master - 您的本地master 尚不存在。)

在以下部分中使用我引用的命令,您将目录中的所有文件暂存(使用 git add . ),但不继续提交它们。然后你似乎被 git 中的一个 bug 击中了 - 命令 git pull 当在尚未提交的存储库中执行时,似乎会清除你的索引(暂存区),恕不另行通知。对我来说这似乎是一个错误的原因是,如果您同样进行了暂存和未提交的更改,但您的 HEAD did 指向提交,则 git pull < ;URL> 将失败,表示这将丢失您的本地更改。在这种情况下,它似乎丢弃了您的暂存文件。 (我可以使用 git 版本 1.7.5.4 重现此内容。)

至于现在该怎么办 - 您在某处还有文件的另一个副本吗?如果您不这样做,并且您确实需要找回它们,那么只要您没有删除 .git 目录,您仍然可以这样做,尽管需要做一些工作自从暂存这些文件以来。 (正如这里其他人评论的那样,一般来说,您永远不应该删除 .git 目录,其中包含整个项目的历史记录。)您可以在此处的答案中找到有关恢复这些暂存文件的一些指导:

  • < a href="https://stackoverflow.com/questions/7374069/undo-git-reset-hard/7376959#7376959">撤消 git reset --hard 中未提交的文件暂存区

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 [email protected]:dchhetri/Demo-s.git

$ git pull [email protected]: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:

邮友 2024-12-17 14:42:21

不应该是这个原因。

将 Git 视为考试中的两个学生。他们知道一些考试答案,并且偶尔会查看彼此的作业,寻找最近的更改,并将相同的更改应用到自己的作业中。

当您从 github 中“拉取”时,它只会复制更改并应用它们。

现在可能删除你的文件的是“git stash”。这会暂时隐藏您的更改,您可以使用“git stash list”查看它们,或者使用“git stash pop”取回它们。

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

凉墨 2024-12-17 14:42:21

点击以下命令来撤消拉动:

git reset --hard HEAD^

Hit the following command to undo the pull:

git reset --hard HEAD^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文