24

I want to remove all stash'es, except most recent, from git stash list.

E.g. I want to remove stash 1 to 3 in a single 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 https://stackoverflow.com/a/5737041/3878940, but its applicable to delete only a single stash. Is there any git command to delete a range of stashes?

Community
  • 1
  • 1
Aditya Singh
  • 15,810
  • 15
  • 45
  • 67

5 Answers5

28

If you want to delete stash 1 to 3,just go to shell and type the following command:

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

Output

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

As git stash uses stack structure, each time you drop nth index, stack indexes decreases by 1. So eventually, you end up dropping stashes 1 to 3. So, like this you can also drop a stash of length n just iterating like :

for n in {1..n}
do
git stash drop stash@{1}   
done
majin
  • 576
  • 1
  • 5
  • 14
  • 1
    The `n` in `{1..n}` in this answer is a placeholder; literally putting `{1..n}` will only execute the drop once. – jayqui May 15 '19 at 23:48
  • @jayqui it will keep dropping the stashes as I already explained that it uses stack structure, so whatever is at top, i.e. 1 here, it will keep dropping till n times. – majin May 17 '19 at 11:05
25

Short answer: no.

Slightly longer answer: no, but it's trivial. You want to drop stashes 1, 2, and 3. When you drop stash #1, stashes 2 and 3 become stashes 1 and 2 respectively. When you drop the new stash #1, stash #2 (which was #3 originally) becomes stash #1. Therefore, to drop three stashes, starting with #1, simply drop stash #1 three times.

torek
  • 448,244
  • 59
  • 642
  • 775
  • 2
    Slight improvement to your answer. Rather, to avoid confusion and adjusting the stash id's while dropping, simply `git stash drop stash@{id}` in **descending order of id's**. So if you want to drop id's 1,2,3 : 'git stash drop stash@{3}' then 'git stash drop stash@{2}' and then 'git stash drop stash@{1}' . – Dhiraj Gandhi Jan 09 '18 at 06:48
  • 1
    @DhirajGandhi: that still causes the stash that was #4 to become #1, in the end. It may help you keep track of how many you have to go, though, in which case it may be of value. – torek Jan 09 '18 at 06:54
  • torek: That's correct, but stash #4 won't be deleted in the end, so there is no problem with the answer of @DhirajGandhi. – AlexS Feb 15 '22 at 08:51
2

If you use Git on PowerShell you can get it easily. There is two approach to achieve this. (n is number of stash)

First approach,

for($i = 1; $i -lt n; $i++){ git stash drop "stash@{1}"}

Second approach,

for($i = n-1; $i -gt 0; $i--){ git stash drop "stash@{$i}"}

I assume you have countless stash, you can get count of stash (n) like this,

git stash list | measure-object -line
0

Supposedly you want to drop all old stashes except the first one you can use a script like this:

for (( ; ; ))
do
    git stash drop stash@{1}
    Result = $?
    if [ Result != 0 ] 
    then
      break
    fi
done

Melchia
  • 22,578
  • 22
  • 103
  • 117
0

POSIX shell and variable range

#!/bin/sh
set -euo

for _ in `seq $1 $2`; do
    git stash drop stash@{$1}
done
CervEd
  • 3,306
  • 28
  • 25