Search code examples
gitversion-controlgit-stash

git stash drop: How can I delete older stashed states without dropping the latest X?


What I already have discoverd:

git stash list

... for listing all my stashes.

git stash show -p stash@{0} --name-only

To list all files within that stash (here the latest stash at position 0).

Now I have a project with hundreds of old stashed changes which will not be needed anymore. I know I could delete them all:

git stash clear

... or delete single stashes like this (deletes the stash with 87 stashes afterwards):

git stash drop stash@{87}

However I would like to delete the stashes 3-107. With a risky guess I tried:

git stash drop stash@{3-107} -- does not work

How can I do this?


Solution

  • Edit: We have to loop backwards because removing a stash changes the index of all stashes.

    git stash drop doesn't accept more than one revision at a time;

    $ git stash drop stash@\{{4..1}\}
    Too many revisions specified: 'stash@{4}' 'stash@{3}' 'stash@{2}' 'stash@{1}'
    

    You could achieve this with a loop in your shell. For example in bash;

    $ for i in {4..1}; do
    >     git stash drop stash@{$i};
    > done
    Dropped stash@{4} (175f810a53b06da05752b5f08d0b6550ca10dc55)
    Dropped stash@{3} (3526a0929dac4e9042f7abd806846b5d527b0f2a)
    Dropped stash@{2} (44357bb60f406d29a5d39ea0b5586578223953ac)
    Dropped stash@{1} (c97f46ecab45846cc2c6138d7ca05348293344ce)