Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 899
Blackbam Avatar asked Sep 26 '18 17:09

Blackbam


2 Answers

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)
like image 170
Adam Avatar answered Sep 27 '22 22:09

Adam


You might try this:

i=3; while [ $i -lt 104 ]; do git stash pop stash@{3}; i=$(( $i + 1 )); done

Always drop 3 because when you drop 3 what was 4 is now 3 and so on so you keep on dropping stash@{3}. Either way use with EXTREME care!

like image 26
eftshift0 Avatar answered Sep 27 '22 22:09

eftshift0