How would I extract a single file (or changes to a file) from a git stash?

GitGit Stash

Git Problem Overview


Is it possible to extract a single file or diff of a file from a git stash without popping the stash changeset off?

Git Solutions


Solution 1 - Git

On the [git stash][git-stash] manpage you can read (in the "Discussion" section, just after "Options" description) that:

> A stash is represented as a commit whose tree records the state of the > working directory, and its first parent is the commit at HEAD when the > stash was created.

So you can treat stash (e.g. stash@{0} is first / topmost stash) as a merge commit, and use:

$ git diff stash@{0}^1 stash@{0} -- <filename>

Explanation: stash@{0}^1 means the first parent of the given stash, which as stated in the explanation above is the commit at which changes were stashed away. We use this form of "git diff" (with two commits) because stash@{0} / refs/stash is a merge commit, and we have to tell git which parent we want to diff against. More cryptic:

$ git diff stash@{0}^! -- <filename>

should also work (see [git rev-parse][git-rev-parse] manpage for explanation of rev^! syntax, in "Specifying ranges" section).

Likewise, you can use [git checkout][git-checkout] to check a single file out of the stash:

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

or to save it under another filename:

$ git show stash@{0}:<full filename>  >  <newfile>

or

$ git show stash@{0}:./<relative filename> > <newfile>

(note that here <full filename> is full pathname of a file relative to top directory of a project (think: relative to stash@{0})).

[git-stash]: http://git-scm.com/docs/git-stash "git-stash - Stash the changes in a dirty working directory away" [git-rev-parse]: http://git-scm.com/docs/git-rev-parse#_specifying_ranges [git-checkout]: http://git-scm.com/docs/git-checkout


You might need to protect stash@{0} from shell expansion, i.e. use "stash@{0}" or 'stash@{0}'.

Solution 2 - Git

If you use git stash apply rather than git stash pop, it will apply the stash to your working tree but still keep the stash.

With this done, you can add/commit the file that you want and then reset the remaining changes.

Solution 3 - Git

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

Notes:

  1. Make sure you put space after the "--" and the file name parameter

  2. Replace zero(0) with your specific stash number. To get stash list, use:

     git stash list
    

Based on Jakub Narębski's answer -- Shorter version

Solution 4 - Git

Edit: See cambunctious's answer, which is basically what I now prefer because it only uses the changes in the stash, rather than comparing them to your current state. This makes the operation additive, with much less chance of undoing work done since the stash was created.

To do it interactively, you would first do

git diff stash^! -- path/to/relevant/file/in/stash.ext perhaps/another/file.ext > my.patch

...then open the patch file in a text editor, alter as required, then do

git apply < my.patch

cambunctious's answer bypasses the interactivity by piping one command directly to the other, which is fine if you know you want all changes from the stash. You can edit the stash^! to be any commit range that has the cumulative changes you want (but check over the output of the diff first).

If applying the patch/diff fails, you can change the last command to git apply --reject which makes all the changes it can, and leaves .rej files where there are conflicts it can't resolve. The .rej files can then be applied using wiggle, like so:

wiggle --replace path/to/relevant/file/in/stash.ext{,.rej}

This will either resolve the conflict, or give you conflict markers that you'd get from a merge.

If your distro doesn't have wiggle, you can just build it:

cd /usr/local/src/
git clone git://git.neil.brown.name/wiggle
cd wiggle/
git checkout v1.3
make install

Previous solution: There is an easy way to get changes from any branch, including stashes:

$ git checkout --patch stash@{0} path/to/file

You may omit the file spec if you want to patch in many parts. Or omit patch (but not the path) to get all changes to a single file. Replace 0 with the stash number from git stash list, if you have more than one. Note that this is like diff, and offers to apply all differences between the branches. To get changes from only a single commit/stash, have a look at git cherry-pick --no-commit.

Solution 5 - Git

Short answer

To see the whole file: git show stash@{0}:<filename>

To see the diff: git diff stash@{0}^1 stash@{0} -- <filename>

Solution 6 - Git

Use the following to apply the changes to a file in a stash to your working tree.

git diff stash^! -- <filename> | git apply

This is generally better than using git checkout because you won't lose any changes you made to file since you created the stash.

Solution 7 - Git

You can get the diff for a stash with "git show stash@{0}" (or whatever the number of the stash is; see "git stash list"). It's easy to extract the section of the diff for a single file.

Solution 8 - Git

The simplest concept to understand, although maybe not the best, is you have three files changed and you want to stash one file.

If you do git stash to stash them all, git stash apply to bring them back again and then git checkout f.c on the file in question to effectively reset it.

When you want to unstash that file run do a git reset --hard and then run git stash apply again, taking advantage ofthe fact that git stash apply doesn't clear the diff from the stash stack.

Solution 9 - Git

If the stashed files need to merge with the current version so use the previous ways using diff. Otherwise you might use git pop for unstashing them, git add fileWantToKeep for staging your file, and do a git stash save --keep-index, for stashing everything except what is on stage. Remember that the difference of this way with the previous ones is that it "pops" the file from stash. The previous answers keep it git checkout stash@{0} -- <filename> so it goes according to your needs.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDannyView Question on Stackoverflow
Solution 1 - GitJakub NarębskiView Answer on Stackoverflow
Solution 2 - GitTim HeniganView Answer on Stackoverflow
Solution 3 - GitRamView Answer on Stackoverflow
Solution 4 - GitWalfView Answer on Stackoverflow
Solution 5 - GitLuboš TurekView Answer on Stackoverflow
Solution 6 - GitcambunctiousView Answer on Stackoverflow
Solution 7 - GitNathan KitchenView Answer on Stackoverflow
Solution 8 - GitPhilluminatiView Answer on Stackoverflow
Solution 9 - GitHola Soy Edu Feliz NavidadView Answer on Stackoverflow