Search code examples
gitgitlabgit-stashrevert

How to recover overwritten file that I git stashed?


I attempted to pull, but git complained about local changes being overwritten so I stashed and pulled. By pulling, I wrote over a file that I now want to revert back to how it was when I stashed it. How can I do this? When I run git git stash show it shows the single file is still there.

I want to keep all other pulled changes in my local repo, I just want to get that single file back to its old state. Thanks


Solution

  • There are several ways. One simple one would be

    git checkout stash -- path/to/the/file
    

    This will copy the file from the stash to your index and working folder. This will replace whatever is currently in your index and working folder; if that's just a version you checked out from the remote and dont' currently want, it's fine, but to be clear: Even if you have changes in the current worktree file that have never been committed, this would overwrite them and they would never be recoverable. So use due discretion with this command.