Fetching a Single File From Git Stash

Sreedev Kodichath
TarkaLabs TIL
Published in
1 min readJun 2, 2020

--

A lot of times when jumping across branches on your Git repository, you’ll come across situations where you need to un-stash a single file from your stashes. Though there is no specific command to achieve this, there are workarounds that can be used to achieve this behavior in Git.

Checking Out a Single File From The Stash

This method will checkout the file from the stash into your working branch. Ensure that there are no local uncommitted changes on the existing file that you do not wish to lose before using this method as it will restore the version of the file as of the time when the stash was performed.

git checkout stash@{0} -- <filename>

Creating a Copy of a Stashed File Under a Different Filename

This is a non-destructive method that will let you recreate the file in the stash into the working branch under a new filename.

git show stash@{0}:stashed_file.rb > copy_of_stashed_file.rb

The stash tool can be used in conjunction with other git features to achieve desired outcomes. I would recommend checking out Atlassian’s documentation on Git Stashing:
https://www.atlassian.com/git/tutorials/saving-changes/git-stash

--

--

Sreedev Kodichath
TarkaLabs TIL

Senior Software Engineer @ Tunecore. Ruby, Elixir, Javascript C & Rust. Amateur Radio Operator — KD2ZWB/VU2JWE. www.sree.dev. Brooklyn, NYC.