Is it possible to preview stash contents in git?

后端 未结 16 1711
后悔当初
后悔当初 2020-12-02 03:34

I often put work away for later, then other stuff comes along, and a few weeks later, I want to inspect the stash, and find out what changes it would make if I applied it to

相关标签:
16条回答
  • 2020-12-02 04:01

    Show all stashes

    File names only:

    for i in $(git stash list --format="%gd") ; do echo "======$i======"; git stash show $i; done
    

    Full file contents in all stashes:

    for i in $(git stash list --format="%gd") ; do echo "======$i======"; git stash show -p $i; done
    

    You will get colorized diff output that you can page with space (forward) and b (backwards), and q to close the pager for the current stash. If you would rather have it in a file then append > stashes.diff to the command.

    0 讨论(0)
  • 2020-12-02 04:02

    When this question was first asked, this may not have been an option, but, if you use PyCharm, you can use the UnStash Changes tool (VCS->Git->UnStash Changes...). This allows you to view the list of stashed changes, as well as pop, drop, clear, or apply (into a new branch if desired):

    and view the changed files per stash:

    as well as diffs per file. In the diffs you can cherry-pick individual changes to apply from the stashed changes to the working branch (using the left-pointing chevron):

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

    yes the best way to see what is modified is to save in file like that:

    git stash show -p stash@{0} > stash.txt
    
    0 讨论(0)
  • 2020-12-02 04:06

    To view all the changes in an un-popped stash:

    git stash show -p stash@{0}
    

    To view the changes of one particular file in an un-popped stash:

    git diff HEAD stash@{0} -- path/to/filename.php
    
    0 讨论(0)
  • 2020-12-02 04:07

    To view a current list of stash:

    git stash list
    

    You'll see a list like this:

    stash@{0}: WIP on ...
    stash@{1}: ...
    stash@{2}: ...
    ...
    

    To view diff on any of those stashes:

    git stash show -p stash@{n}
    
    0 讨论(0)
  • 2020-12-02 04:14

    I like how gitk can show you exactly what was untracked or sitting in the index, but by default it will show those stash "commits" in the middle of all your other commits on the current branch.

    The trick is to run gitk as follows:

    gitk "stash@{0}^!"
    

    (The quoting is there to make it work in Powershell but this way it should still work in other shells as well.)

    If you look up this syntax in the gitrevisions help page you'll find the following:

    The r1^! notation includes commit r1 but excludes all of its parents. By itself, this notation denotes the single commit r1.

    This will apparently put gitk in such a mode that only the immediate parents of the selected commit are shown, which is exactly what I like.


    If you want to take this further and list all stashes then you can run this:

    gitk `git stash list '--pretty=format:%gd^!'`
    

    (Those single quotes inside the backticks are necessary to appease Bash, otherwise it complains about the exclamation mark)

    If you are on Windows and using cmd or Powershell:

    gitk "--argscmd=git stash list --pretty=format:%gd^!"
    
    0 讨论(0)
提交回复
热议问题