Search code examples
gitgit-stash

Preview changes stash will make to current HEAD without applying


As the answers to this question show you can view the changes of a stash with:

git stash show -p stash@{N}

However, that compares the stash to its original parent, and does not indicate what you would be left with after applying the stash (e.g the stash may have already been merged in or there maybe conflicts).

Is there an easy way of previewing those changes without applying the stash and performing a diff (as this leaves your current working state 'dirty')?


Solution

  • A quick way to preview the result of a applying a stash would be to create a topic branch:

    git checkout -b teststash
    

    After you apply the stash it would be trivial to either "accept" or "deny" the change.

    To "deny" the change, simply remove the changes and go back to your previous branch:

    git checkout . # or re-stash if you still want that patch
    git checkout - # switch back to previous branch
    

    To "accept" the change simply make a commit, switch back to your previous branch, and merge in your temp branch. It'll be a fast-forward merge so your history won't indicate that you had the preview branch at all (which you probably wouldn't care about later):

    git add path/to/file.txt
    git commit -m "re-apply fix foo"
    git checkout -
    git merge -
    

    Or, if you want to "accept" the change but don't want to commit at that point, simply switch back to your original branch and keep on working:

    git checkout -
    

    So, in summary, there is not a built in "preview after I apply" Git command, but Git's light-weight branching makes this a pretty easy action.