Git stash: “Cannot apply to a dirty working tree, please stage your changes”

后端 未结 11 1652
青春惊慌失措
青春惊慌失措 2020-12-12 09:04

I am trying to apply changes I stashed earlier with git stash pop and get the message:

Cannot apply to a dirty working tree, please stage your c         


        
相关标签:
11条回答
  • 2020-12-12 09:21

    I do it in this way:

    git add -A
    git stash apply
    

    and then (optionaly):

    git reset
    
    0 讨论(0)
  • 2020-12-12 09:21

    You can do this without having to stash your current changes by exporting the stash you want as a patch file and manually applying it.

    For example, say you want to apply stash@{0} to a dirty tree:

    1. Export stash@{0} as a patch:

      git stash show -p stash@{0} > Stash0.patch

    2. Manually apply the changes:

      git apply Stash0.patch

    If the second step fails, you will have to edit the Stash0.patch file to fix any errors and then try git apply again.

    0 讨论(0)
  • 2020-12-12 09:22

    I was unable to get most of these to work; for some reason it always thinks I have local changes to a file. I can't apply a stash, patches won't apply, checkout and reset --hard fail. What finally worked was saving the stash as a branch with git stash branch tempbranchname, and then doing a normal branch merge: git checkout master and git merge tempbranchname. From http://git-scm.com/book/en/Git-Tools-Stashing :

    If you want an easier way to test the stashed changes again, you can run git stash branch, which creates a new branch for you, checks out the commit you were on when you stashed your work, reapplies your work there, and then drops the stash if it applies successfully

    0 讨论(0)
  • 2020-12-12 09:30

    None of these answers actually work if you find yourself in this situation as I did today. Regardless of how many git reset --hard's I did, it got me nowhere. My answer (not official by any means was):

    1. Figure out the stash's hash use git reflog --all
    2. Merge that hash with the branch you're interested in
    0 讨论(0)
  • 2020-12-12 09:30

    You have files that have been modified but not committed. Either:

    git reset --hard HEAD (to bring everything back to HEAD)
    

    or, if you want to save your changes:

    git checkout -b new_branch
    git add ...
    git commit
    git checkout -b old_branch
    git stash pop
    
    0 讨论(0)
提交回复
热议问题