1097

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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Danny
  • 13,194
  • 4
  • 31
  • 36

10 Answers10

1548

On the 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 manpage for explanation of rev^! syntax, in "Specifying ranges" section).

Likewise, you can use 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})).


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

James Skemp
  • 8,018
  • 9
  • 64
  • 107
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
  • 19
    This is pretty cool... I didn't _really_ understand how stash worked until I read your answer (which lead me to the git-checkout addition). I really didn't get that, when you do a stash, git saves TWO commits -- one for the state of the index and one for the state of the working copy which is a merge between the index and the original HEAD. This explains the odd trees I've seen when I visualize the repository with "gitk --all" when stashes are present. – Pat Notz Jul 09 '09 at 20:56
  • 6
    Mostly I find the git checkout application to be the best way to accomplish what it is I wanted to do. However I was curious and double-checked `git checkout`'s man page. It cannot drop the file into another location. There is a reference to this in: http://stackoverflow.com/questions/888414/git-checkout-older-revision-of-a-file-under-a-new-name/888623#888623 – Danny Jul 09 '09 at 22:15
  • 67
    Note that the `git checkout` approach copies the _exact_ file from the stash -- it doesn't merge it with what's in your working directory like `git stash apply` would. (So if you have any changes from the base the stash was created on, they'll be lost). – peterflynn Apr 05 '13 at 22:04
  • 6
    Note that in order for `git stash apply` to merge the changes in a file that has been modified in the work tree since the file was stashed, that file in the work tree must be staged. For auto-merge to work, the same files cannot be modified both in the working copy and in the stashed copy-to-be-merged. Finally, stash apply doesn't remove the item from stash like `git stash pop` would. – Ville Nov 04 '13 at 16:15
  • 4
    `git diff stash@{0}^1 stash@{0} -- | patch -p1` – isaaclw Oct 31 '14 at 01:42
  • 3
    git checkout "stash@{0}" if you get "fatal: invalid reference: stash@0" – Frank Fang Jul 27 '16 at 09:21
  • 2
    `git checkout stash@{0} -- ` can checkout folders too! – Dziamid Oct 28 '16 at 07:35
  • +1 to @isaaclw's answer using `patch -p1`. I had tried `git diff … | git apply` from http://stackoverflow.com/a/15264717/1172352, but that failed with the error "patch does not apply." I tried `git diff … | patch -p1` instead and it applied the diff from the stash perfectly! – peterflynn Jan 04 '17 at 01:09
  • 3
    I had to enclose in double quotes on Windows: "stash@{0}:" – Mike W Jan 16 '18 at 19:40
  • 2
    At least if there is just one item in the stash list, it works fine to type simply `git checkout stash -- ` without the '@{0}' after 'stash'. (It might work with multiple stashed states, too, but I have not tried it and do not know which state would be the default.) – Otto G Mar 15 '19 at 14:04
  • As per @OttoG's answer, if you're only interested in the most recent stash item, you can omit the `@{0}` – camslice Jun 11 '20 at 07:55
  • Really helpful answer. Really liked git checkout stash@{0} -- – Jamesed May 27 '21 at 12:53
  • 1
    Can't use checkout with untracked files. – Nomas Prime Jul 02 '21 at 14:34
  • `$ git checkout stash@{0} -- ` stages the file for commit. Can that be avoided as quite often file is taken from stash just for tests? – Danijel Sep 22 '21 at 08:45
  • 1
    @Danijel with modern Git you can use `$ git restore --worktree --source=stash@{0} -- `, I think. – Jakub Narębski Sep 23 '21 at 10:07
  • 2
    In PowerShell it is necessary to put `stash@{0}` to single quotes or backticks. Thats because curly braces have special meaning in PowerShell. Otherwise you'll get ``error: unknown switch `e'``. – Pavol Hlavatý Oct 07 '21 at 11:27
  • 1
    is there any way to address untracked files in a stash with any of those methods @JakubNarębski? I tried `checkout` which does not find untracked files apparently – vrms Feb 28 '22 at 08:20
  • 2
    @vrms: **If** you have saved untracked files to stash by using the `--include-utracked`, then the tree with _untracked files_ is available as `stash@{0}^2`, that is _second parent of stash_ (so you can for example use `git checkout 'stash@{0}^2' -- `). You can see this with `git log --graph --oneline stash`. – Jakub Narębski Mar 01 '22 at 09:26
84

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.

Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
  • 4
    To pop a specific stash: `git stash pop stash@{0}` (list the stashed changes: `git stash list`) – MrYoshiji Jan 15 '15 at 16:53
  • If other files in the stash cause a merge conflict, git doesn't let me commit on the file I want :( – cambunctious Dec 07 '19 at 23:45
  • 4
    This is the best answer. You don't have to use a *diff*erent command (heh) to do what git stash apply should do, and you don't have to worry about any weird side effects (git checkout copies the exact file to the stash) or memorize any weird syntax. – Patrick May 25 '22 at 21:40
79
$ 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

Pang
  • 9,564
  • 146
  • 81
  • 122
Ram
  • 15,908
  • 4
  • 48
  • 41
  • 1
    You can also use the stash name `git checkout stash^{/'Stash name here'} -- ` – Kerry Johnson Feb 10 '21 at 16:50
  • 2
    This has two risks: (a) it will silently overwrite any unstaged local changes you have in this file; and (b) it copies the _exact_ file from the stash, without merging it with any changes that have been made on your branch since the stash was saved. That may be confusing since `git stash apply` / `pop` don't have either of those pitfalls. To get behavior that is more expected, use one of the `git diff`-based approaches in some of the other answers here. – peterflynn Jun 22 '22 at 20:06
47

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.

Walf
  • 8,535
  • 2
  • 44
  • 59
  • 1
    Does this copy the exact file from the stash, or does it merge? In case of copy, if you have any changes since the stash was created, they will be lost. – Danijel Oct 30 '18 at 11:39
  • 3
    @Danijel Read `git help checkout`. `--patch` does interactive merging, It applies whatever hunk(s) you approve in the shell (or whatever you save if you choose to `e`dit the patch). Path alone will overwrite the file, like I wrote, "all changes". – Walf Oct 31 '18 at 06:45
  • 1
    slight improvement: `git config --global alias.applydiffat '!git checkout --patch "$1" -- $(git diff --name-only "$1"^ "$1")'` -- then doing `git applydiffat stash@{4}` only uses files that changed between the stash and its parent. – Mark May 08 '20 at 18:38
36

Short answer

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

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

Luboš Turek
  • 6,273
  • 9
  • 40
  • 50
  • 1
    I don't think s/he was concerned with viewing the stash for a particular file--only popping that file. – Alkanshel Apr 01 '16 at 18:29
  • 3
    This is what I was looking for. Then you can replace `diff` with `difftool` to use your favourite external diff. – Peet Brits Jan 17 '17 at 10:36
  • 1
    My git repo was corrupted but I had stashed the files, but couldn't apply because of the corruption. I was able to use this and pipe to a new file, diff it, and I was good to go. Thank you! – Adam Tuliper Nov 24 '20 at 21:16
19

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.

cambunctious
  • 8,391
  • 5
  • 34
  • 53
11

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.

Nathan Kitchen
  • 4,799
  • 1
  • 24
  • 19
  • 5
    Simpler still for minds like me: use `git show stash` to show the topmost stash (normally the only one you have). Similarly you can show the diff between your current branch and the stash with `git diff head stash`. – Dizzley Nov 02 '13 at 10:00
5

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.

Philluminati
  • 2,649
  • 2
  • 25
  • 32
1

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.

1

For VS Code users, there is one method. Make sure to have GitLens extension installed.

  1. Go on SOURCE CONTROL tab
  2. Click on STASHES.
  3. You will be able to see available stashes.
  4. Click on the desired stash.
  5. Right click on the desired file which you want to unstash.
  6. You will get 2 options, Apply Changes and Restore (Checkout). You can click any of the options and you will get your file under Changes if you choose Apply Changes or Staged Changes if you choose Restore (Checkout).

unstash files from stash

Akshay
  • 926
  • 9
  • 21