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?
There are two ways to restore work from the stash: Using git stash pop - The command restores the changes and schedules the stash for deletion. Using git stash apply - The command restores the changes but doesn't delete the stash from the ref.
git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.
You can reapply stashed changes with the commands git stash apply and git stash pop . Both commands reapply the changes stashed in the latest stash (that is, stash@{0} ). A stash reapplies the changes while pop removes the changes from the stash and reapplies them to the working copy.
git stash apply vs pop : They are almost similar except the fact that git stash pop throws away the (topmost, by default) stash when applying it, whereas git stash apply leaves it within the stash list for possible later use (or you'll then git stash drop it).
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With