2

I have the list of stashes and want to drop some of them selectively.

For my example, I want to remove stash@{1}, stash@{3}, stash@{4}, stash@6}

$git stash list

stash@{1}: Tests On branch1-om: Test for #8
stash@{2}: WIP On branch1-om: WIP for #12
stash@{3}: Temp on branch1-om: 0a447303 Add Unit Tests for the HttpClient
stash@{4}: To delete stash: 233abc813c fix
stash@{5}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{6}: On branch-test-om: projects deleted/modified when rebuilt
stash@{7}: My configurations: Apply my local dev configurations

My Current step is very repetitive:

$git stash drop stash@{1}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: Temp on branch1-om: 0a447303 Add Unit Tests for the HttpClient
stash@{3}: To delete stash: 233abc813c fix
stash@{4}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{5}: On branch-test-om: projects deleted/modified when rebuilt
stash@{6}: My configurations: Apply my local dev configurations

$git stash drop stash@{2}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: To delete stash: 233abc813c fix
stash@{3}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{4}: On branch-test-om: projects deleted/modified when rebuilt
stash@{5}: My configurations: Apply my local dev configurations

$git stash drop stash@{2}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{3}: On branch-test-om: projects deleted/modified when rebuilt
stash@{4}: My configurations: Apply my local dev configurations

$git stash drop stash@{3}

$git stash list

stash@{1}: WIP On branch1-om: WIP for #12
stash@{2}: WIP on branchn-test-om: 4a42e4 WIP: Commit
stash@{4}: My configurations: Apply my local dev configurations
0

2 Answers 2

7

The problem is that removing stashes from the top renumbers them. But since you can remove them at any position, you don't need to act as if stashes have only stack-like access.

Drop them from the furthest, to keep the numbers intact:

for N in 6 4 3 1; do git stash drop stash@\{$N}; done
4
  • 1
    Note that recent versions of git stash let you use the number without the stash@{...} part, too. If your Git version predates 2.11, you'll need the longer form, though.
    – torek
    Commented Dec 11, 2019 at 18:32
  • @torek: good to know! But the man page for version 2.24 still only shows the old syntax in examples, though it does mention the new syntax, and also power-user things like stash@{2.hours.ago}.
    – 9000
    Commented Dec 11, 2019 at 18:49
  • @9000 is the temp solution that we can take now... not approving as accepted answer as it doesn't completely provide the solution...
    – OmGanesh
    Commented Jan 14, 2020 at 21:19
  • 1
    Yes, I changed my mind to go with this as solution, as of now, until git provides new APIs for bulk drop. Minor update as suggested by torek: "$ for N in 6 4 3 1; do git stash drop $N; done"
    – OmGanesh
    Commented Jan 29, 2020 at 1:27
0

Unfortunately, git stash drop seems to not support multiple stash as parameters:

% git stash drop stash@{{0},{2}}
Too many revisions specified: 'stash@{0}' 'stash@{2}'

My best guess would be to do:

i=0; for s in 0 2; do git stash drop stash@{$((s-i))}; i=$((i+1)); done

Because git stash drop updates the stash index even if you don't look at the stash list (which is odd tbh)

EDIT: Although this is only if you want to remove them in ascending order, 9000's solution is way simpler otherwise.

Not the answer you're looking for? Browse other questions tagged or ask your own question.