奇怪的 git 案例 - git stash 后跟 git stash apply 丢失了未提交的数据?

发布于 2024-08-28 08:39:31 字数 189 浏览 5 评论 0原文

我有一个文件,假设 file.txt 我已经完成了 git mv file.txt 到 file1.txt,然后我创建了一个名为 file.txt 的新文件并对其进行了处理。不幸的是我还没有将该文件添加到 git 中。不管怎样,问题是我做了 git stash,然后 git stash apply,但是新的 file.txt 消失了......无论如何要找回它?

I have a file, let's say file.txt I have done git mv file.txt to file1.txt, then I created a new file called file.txt and worked on it. Unfortunately I didn't add that file to git yet. Anyway the problem is that I did git stash, then git stash apply, but the new file.txt disappeared... anyway to get it back?

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

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

发布评论

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

评论(4

温暖的光 2024-09-04 08:39:31

这里的问题主要是对 git stash save 的作用的误解。它仅保存对跟踪文件的更改。未跟踪的文件不会由 git stash 保存。当您将 file.txt 移动到 file1.txt 时,新的 file.txt 是一个未跟踪的文件,不会git stash 保存。这不是一个错误,这只是 git stash 的行为方式。 git stash 的文档可能对此更清楚。

正如 git stash save 的文档所述,保存更改后它将执行 git reset --hard 。正是 git reset --hard 覆盖了新的 file.txt。有人可能会争辩说,如果未跟踪的文件将被覆盖, git reset --hard 应该生成警告,但我仍然不认为这是一个错误。它正在做它应该做的事情。

这里要理解的重要一点 - 以及什么可以为你省去很多麻烦 - 是 git stash save 不保存未跟踪的文件(而且它可能应该' t)。

The problem here is mostly a misunderstanding of what git stash save does. It saves only changes to tracked files. Untracked files are not saved by git stash. When you moved file.txt to file1.txt, the new file.txt is an untracked file and will not be saved by git stash. This isn't a bug, it's just the way that git stash behaves. It could be that the documentation for git stash should be more clear about this.

As the documentation for git stash save states, it will do a git reset --hard after saving your changes. It was the git reset --hard that overwrote the new file.txt. One might argue that git reset --hard should generate a warning if an untracked file will be overwritten, but I still wouldn't call this a bug. It's doing what it's supposed to do.

The important thing to understand here -- and what would have saved you a lot of trouble -- is that git stash save does not save untracked files (and it probably shouldn't).

忘你却要生生世世 2024-09-04 08:39:31

这看起来像是存储中的严重(即数据丢失)错误。请举报。不幸的是,我不相信有任何方法可以恢复新的 file.txt

此错误现已在 git >=1.7.1.1 中修复。

This looks like serious (i.e. data loss) bug in stash. Please report it. Unfortunately, I don't believe that there's any way to get the new file.txt back.

This bug has now been fixed in git >=1.7.1.1.

情释 2024-09-04 08:39:31

为了将来,使用 git stash -u 来存储未提交的文件 (http://www.kernel.org/pub/software/scm/git/docs/git-stash.html) 你可以这样做我相信这是从 git 版本 1.7 开始的。

for the future, use git stash -u to stash uncommitted files (http://www.kernel.org/pub/software/scm/git/docs/git-stash.html) You can do this starting from git version 1.7 i believe.

和我恋爱吧 2024-09-04 08:39:31

这篇文章只是为了简单地说明游戏过程,而不是塞进评论中。 注意:使用 Git 版本 1.7.0.2

重新创建:

~/test $ git init
~/test $ echo "hello" > file.txt
~/test $ git add .
~/test $ git commit -m "init commit"

~/test $ git mv file.txt file1.txt
~/test $ echo "new data" > file.txt
~/test $ git stash
~/test $ git stash apply

~/test $ cat file.txt
cat: file.txt: No such file or directory

~/test $ cat file1.txt
hello

This is post is to simply illustrate the recreation process without getting crammed into a comment. Note: Using Git version 1.7.0.2

To recreate:

~/test $ git init
~/test $ echo "hello" > file.txt
~/test $ git add .
~/test $ git commit -m "init commit"

~/test $ git mv file.txt file1.txt
~/test $ echo "new data" > file.txt
~/test $ git stash
~/test $ git stash apply

~/test $ cat file.txt
cat: file.txt: No such file or directory

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