How to remove git stash range?

I want to delete all but the last stash'es from git stash list.

eg. I want to remove stash from 1 to 3 in one git command:

stash@{0}: On master: Test related changes
stash@{1}: On master: Tets
stash@{2}: On master: Integrate bunyan logging and http2
stash@{3}: On master: Integrate bunyan logging and http2

I checked this answer to https://stackoverflow.com/a/212960/2125 , but applied it to remove only one cache. Is there a git command to remove the stashes range ?

+13
source share
3 answers

The short answer is no.

: , . 1, 2 3. 1, 2 3 1 2 . stash # 1, stash # 2 ( № 3 ) stash # 1. , , №1, stash # 1.

+18

stash 1 3, :

for n in {1..3}
do
git stash drop stash@{1}   
done

Dropped stash@{1} (79f369e9c4ce8348af8bd2da63f384cc7d02655e)
Dropped stash@{1} (744d2fc40e25f2db1bdc182d41f6eb9134957df4)
Dropped stash@{1} (7f9989207a675549866ab1fc7b15082eb4161e9f)

git stash , , n- , 1. , 1 3. , n, :

for n in {1..n}
do
git stash drop stash@{1}   
done
+16

I would add something additional to the answer below. If you also want to register the index of discarded stashs, you can use the descending order and $n, as shown below

for n in {3..1}; do git stash drop stash@{$n}; done;

The output will be something like ..

Dropped stash@{3} (beeaf3629b53d612b1a3fabb7b0c4f77d17e9b71)
Dropped stash@{2} (5a687ca9a93b3190882a9af2c15134e75f063f41)
Dropped stash@{1} (e33dfc42d03166def5dbb60baa975efb21b27f07)
+2
source

Source: https://habr.com/ru/post/1667785/


All Articles