How to reverse apply a stash?

前端 未结 11 2017
忘掉有多难
忘掉有多难 2020-11-29 14:32

I have a small patch saved away in my git stash. I\'ve applied it to my working copy using git stash apply. Now, I\'d like to back out those changes by revers

相关标签:
11条回答
  • 2020-11-29 15:23

    I had a similar issue myself, I think all you need to do is git reset --hard and you will not lose your changes or any untracked changes.

    If you read the docs in git stash --help it states that apply is "Like pop, but do not remove the state from the stash list" so the state still resides there, you can get it back.

    Alternatively, if you have no conflicts, you can just git stash again after testing your changes.

    If you do have conflicts, don't worry, git reset --hard won't lose them, as "Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards."

    0 讨论(0)
  • 2020-11-29 15:24

    How to reverse apply a stash?

    Apart from what others have mentioned, easiest way is first do

    git reset HEAD
    

    and then checkout all local changes

    git checkout . 
    
    0 讨论(0)
  • 2020-11-29 15:33

    The V1 git man page had a reference about un-applying a stash. The excerpt is below.

    The newer V2 git man page doesn't include any reference to un-applying a stash but the below still works well

    Un-applying a Stash In some use case scenarios you might want to apply stashed changes, do some work, but then un-apply those changes that originally came from the stash. Git does not provide such a stash un-apply command, but it is possible to achieve the effect by simply retrieving the patch associated with a stash and applying it in reverse:

    $ git stash show -p stash@{0} | git apply -R
    

    Again, if you don’t specify a stash, Git assumes the most recent stash:

    $ git stash show -p | git apply -R
    

    You may want to create an alias and effectively add a stash-unapply command to your Git. For example:

    $ git config --global alias.stash-unapply '!git stash show -p | git apply -R'
    $ git stash apply
    $ #... work work work
    $ git stash-unapply
    
    0 讨论(0)
  • 2020-11-29 15:35

    This is in addition to the above answers but adds search for the git stash based on the message as the stash number can change when new stashes are saved. I have written a couple of bash functions:

    apply(){
      if [ "$1" ]; then
        git stash apply `git stash list | grep -oPm1 "(.*)(?=:.*:.*$1.*)"`
      fi
    }
    remove(){
      if [ "$1" ]; then
        git stash show -p `git stash list | grep -oPm1 "(.*)(?=:.*:.*$1.*)"` | git apply -R
        git status
      fi
    }
    
    1. Create stash with name (message) $ git stash save "my stash"
    2. To appply named $ apply "my stash"
    3. To remove named stash $ remove "my stash"
    0 讨论(0)
  • 2020-11-29 15:36

    git stash[save] takes your working directory state, and your index state, and stashes them away, setting index and working area to HEAD version.

    git stash apply brings back those changes, so git reset --hard would remove them again.

    git stash pop brings back those changes and removes top stashed change, so git stash [save] would return to previous (pre-pop) state in this case.

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