How to unstash only certain files?

后端 未结 7 973
醉梦人生
醉梦人生 2020-12-04 04:29

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

相关标签:
7条回答
  • 2020-12-04 05:00

    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>
    

    With Git 2.23+ (August 2019), use git restore, which replaces the confusing git checkout command:

    git restore -s stash@{0} -- <filename>
    

    That does overwrite filename: make sure you didn't have local modifications, or you might want to merge the stashed file instead.

    (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".

    0 讨论(0)
  • 2020-12-04 05:03

    First list all the stashes

    git stash list
    

    stash@{0}: WIP on Produktkonfigurator: 132c06a5 Cursor bei glyphicon plus und close zu zeigende Hand ändern
    stash@{1}: WIP on Produktkonfigurator: 132c06a5 Cursor bei glyphicon plus und close zu zeigende Hand ändern
    stash@{2}: WIP on master: 7e450c81 Merge branch 'Offlineseite'
    

    Then show which files are in the stash (lets pick stash 1):

    git stash show 1 --name-only
    
    //Hint: you can also write
    //git stash show stash@{1} --name-only
    

     ajax/product.php
     ajax/productPrice.php
     errors/Company/js/offlineMain.phtml
     errors/Company/mage.php
     errors/Company/page.phtml
     js/konfigurator/konfigurator.js
    

    Then apply the file you like to:

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

    or whole folder:

    git checkout stash@{1} /errors
    

    It also works without -- but it is recommended to use them. See this post.

    It is also conventional to recognize a double hyphen as a signal to stop option interpretation and treat all following arguments literally.

    0 讨论(0)
  • 2020-12-04 05:09

    One more way:

    git diff stash@{N}^! -- path/to/file1 path/to/file2  | git apply -R
    
    0 讨论(0)
  • 2020-12-04 05:12
    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
    
    0 讨论(0)
  • 2020-12-04 05:13

    If you git stash pop (with no conflicts) it will remove the stash after it is applied. But if you git stash apply it will apply the patch without removing it from the stash list. Then you can revert the unwanted changes with git checkout -- files...

    0 讨论(0)
  • 2020-12-04 05:13

    For Windows users: curly braces have special meaning in PowerShell. You can either surround with single quotes or escape with backtick. For example:

    git checkout 'stash@{0}' YourFile

    Without it, you may receive an error:

    Unknown switch 'e'

    0 讨论(0)
提交回复
热议问题