奇怪的 git 案例 - git stash 后跟 git stash apply 丢失了未提交的数据?
我有一个文件,假设 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的问题主要是对 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 bygit stash
. When you moved file.txt to file1.txt, the new file.txt is an untracked file and will not be saved bygit stash
. This isn't a bug, it's just the way thatgit stash
behaves. It could be that the documentation forgit stash
should be more clear about this.As the documentation for
git stash save
states, it will do agit reset --hard
after saving your changes. It was thegit reset --hard
that overwrote the new file.txt. One might argue thatgit 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).这看起来像是存储中的严重(即数据丢失)错误。请举报。不幸的是,我不相信有任何方法可以恢复新的
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.
为了将来,使用 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.这篇文章只是为了简单地说明游戏过程,而不是塞进评论中。 注意:使用 Git 版本 1.7.0.2
重新创建:
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: