Aborting a stash pop in Git

后端 未结 14 2247
鱼传尺愫
鱼传尺愫 2020-12-22 17:47

I popped a stash and there was a merge conflict. Unlike the question that is listed as a duplicate, I already had some uncommitted changes in the directory which I wanted to

14条回答
  •  臣服心动
    2020-12-22 17:57

    Ok, I think I have worked out "git stash unapply". It's more complex than git apply --reverse because you need reverse merging action in case there was any merging done by the git stash apply.

    The reverse merge requires that all current changes be pushed into the index:

    • git add -u

    Then invert the merge-recursive that was done by git stash apply:

    • git merge-recursive stash@{0}: -- $(git write-tree) stash@{0}^1

    Now you will be left with just the non-stash changes. They will be in the index. You can use git reset to unstage your changes if you like.

    Given that your original git stash apply failed I assume the reverse might also fail since some of the things it wants to undo did not get done.

    Here's an example showing how the working copy (via git status) ends up clean again:

     $ git status
    # On branch trunk
    nothing to commit (working directory clean)
     $ git stash apply
    Auto-merging foo.c
    # On branch trunk
    # Changed but not updated:
    #   (use "git add ..." to update what will be committed)
    #   (use "git checkout -- ..." to discard changes in working directory)
    #
    #       modified:   foo.c
    #
    no changes added to commit (use "git add" and/or "git commit -a")
     $ git add -u
     $ git merge-recursive stash@{0}: -- $(git write-tree) stash@{0}^1
    Auto-merging foo.c
     $ git status
    # On branch trunk
    nothing to commit (working directory clean)
    

提交回复
热议问题