可以使用“git rm -r -f<目录>”吗?命令被恢复?

发布于 2024-12-31 23:54:04 字数 748 浏览 2 评论 0原文

我正在设置我的第一个 git 存储库,并执行了以下操作,

git init

然后是一些 git-config 内容来设置远程服务器。然后我做了

git add directory
git status

哎呀,我添加了一些我不想要的文件。好的,所以我应该 git rm 从提交列表中删除该目录并重新开始

git rm directory

此时我应该正确阅读控制台消息和文档......但没有。于是我就跑了

git rm directory -r -f

嗯?我的目录去哪儿了?啊,好吧,git 已经删除了它,所以它“不存在”了。所以让我们尝试一下,

git status
git reset --hard

在没有成功、一些错误消息和一堆网络搜索之后,我意识到我的失礼。我应该使用

git rm -r --cached directory

它将其从提交列表中删除,但从我的文件系统中删除。哎呀。幸运的是没有造成严重损失。

似乎应该有一种方法可以从中恢复,但我的大多数搜索最终都指向“--cached”选项......而且现在有点晚了。没有提交,所以我不能只恢复/拉取文件(只有本地副本)。

是否可以找回这些文件?

I was setting up my first git repository and did the following,

git init

follow by some git-config stuff to setup the remote server. I then did

git add directory
git status

Whoops, I added some files which I did not want. Okay, so I should git rm to remove the directory from the commit list and start again

git rm directory

At this point I should have read the console message and documentation properly....but didn't. So I ran

git rm directory -r -f

Huh? Where did my directory go? Ah, okay, git has removed it so it is "not there" any more. So lets try,

git status
git reset --hard

After no success, some error messages and a bunch of web searches, I realised my faux pas. I should have used

git rm -r --cached directory

which would have removed it from the commit list, but not from my file system. Whoops. Fortunately nothing serious lost.

It seems like there should be a way to recover from this, but most of my searches end up pointing to the "--cached" option...and it is a bit late for that. There are no commits, so I can't just revert/pull the files (there was only a local copy).

Is it possible to get those files back?

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

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

发布评论

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

评论(2

何处潇湘 2025-01-07 23:54:04

没有提交,所以我不能只恢复/拉取文件(只有本地副本)

并且

是否可以找回这些文件?

答案是

没有提交,因此您没有使用源代码控制。

这些文件从未进入对象数据库。一般来说,你永远无法在 git 中取回未提交的内容。您通常可以取回之前提交的内容。

经验法则:经常提交

编辑

另请参阅

我今天也不小心在我的存储库上运行了 git reset --hard,同时也有未提交的更改。为了取回它,我运行了 git fsck --lost-found ,它将所有未引用的 blob 写入 /.git/lost-found/。由于这些文件未提交,我在 /.git/lost-found/ 内的 other 目录中找到了它们。从那里,我可以看到未提交的文件,复制 blob,然后重命名它们。

注意:只有您添加了要保存到索引的文件(使用 git add .)时,此操作才有效。如果文件不在索引中,它们就会丢失。

There are no commits, so I can't just revert/pull the files (there was only a local copy)

and

Is it possible to get those files back?

The answer is no.

There are no commits, hence you are not using source control.

The files never entered the object database. In general, you can never get uncomitted stuff back in git. You can most often get previously committed stuff back.

Rule of thumb: commit often

Edit

See also

I accidentally ran git reset --hard on my repo today too while having uncommitted changes too today. To get it back, I ran git fsck --lost-found, which wrote all unreferenced blobs to <path to repo>/.git/lost-found/. Since the files were uncommitted, I found them in the other directory within the <path to repo>/.git/lost-found/. From there, I can see the uncommitted files, copy out the blobs, and rename them.

Note: This only works if you added the files you want to save to the index (using git add .). If the files weren't in the index, they are lost.

笨死的猪 2025-01-07 23:54:04

我犯了完全相同的错误,但尽管之前将文件添加到 git 索引,但我无法使用“git fsck --lost-found”恢复它们。幸运的是,我记得 IDE 中的“本地历史记录”跟踪选项,因此使用它,毫无问题地恢复了除库二进制文件之外的所有代码:)

I did exactly the same mistake, but in spite of adding files to the git index earlier, I couldn't recover them using 'git fsck --lost-found'. Luckily, I remembered the 'Local history' tracking option in my IDE so using it, without no problem I recovered all the code except of library binaries :)

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