How to unstash only certain files?

I stashed my changes. Now I want to unstash only some files from the stash. How can I do this?


As mentioned below, and detailed in "How would I extract a single file (or changes to a file) from a git stash?", you can apply use git checkout or git show to restore a specific file.

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

(As commented by Jaime M., for certain shell like tcsh where you need to escape the special characters, the syntax would be: git checkout 'stash@{0}' -- <filename> )

or to save it under another filename:

git show stash@{0}:<full 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} )).

yucer suggests in the comments:

If you want to select manually which changes you want to apply from that file:

git difftool stash@{0}..HEAD -- <filename>

Vivek adds in the comments:

Looks like " git checkout stash@{0} -- <filename> " restores the version of the file as of the time when the stash was performed -- it does NOT apply (just) the stashed changes for that file.
To do the latter:

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

(as commented by peterflynn, you might need | git apply -p1 in some cases, removing one ( p1 ) leading slash from traditional diff paths)

As commented: "unstash" ( git stash pop ), then:

  • add what you want to keep to the index ( git add )
  • stash the rest: git stash --keep-index
  • The last point is what allows you to keep some file while stashing others.
    It is illustrated in "How to stash only one file out of multiple files that have changed".


    git checkout stash@{N} <File(s)/Folder(s) path> 
    

    Eg. To restore only ./test.c file and ./include folder from last stashed,

    git checkout stash@{0} ./test.c ./include
    

    我认为VonC的答案可能是你想要的,但是这里有一种方法来做一个选择性的“git apply”:

    git show stash@{0}:MyFile.txt > MyFile.txt
    
    链接地址: http://www.djcxy.com/p/24500.html

    上一篇: Git Shelve vs藏匿

    下一篇: 如何仅固定某些文件?