2060

How can I delete all of my Git stashes at once?

Specifically I mean, with typing in one command.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Rebekah Waterbury
  • 22,236
  • 5
  • 23
  • 28

12 Answers12

3525

The following command deletes all your stashes:

git stash clear

From the git documentation:

clear

Remove all the stashed states.

IMPORTANT WARNING: Those states will then be subject to pruning, and may be impossible to recover (...).

Community
  • 1
  • 1
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • 17
    why not git stash drop ? – user20358 Sep 02 '15 at 11:48
  • 219
    @user20358: Because `stash drop` (" _Remove a single stashed state from the stash list. When no is given, it removes the latest one. (...)_ ") does not answer the question (" _How can I delete **all** of my Git stashes at once?_ ")? – Tadeck Sep 02 '15 at 15:48
  • 6
    @Tadeck, ok, `stash drop` *removes a single stashed state*. But `stash clear` has this *Note that those states will then be subject to pruning, and may be impossible to recover*. And `stash drop` doesn't have that text. So one can easily think that these commands don't do the same thing and have some big differece. – Green Nov 02 '16 at 01:36
  • 24
    In addition to what accepted answer mentioned - `git stash clear`, to confirm if all cleared, use - `git stash list` – kamal Sep 20 '17 at 08:15
  • 2
    @jedd.ahyoung This should have another 1,000 upvotes. The documentation for `git` is practically impossible to find! https://git-scm.com/docs/git-stash – Drenai Jan 03 '18 at 10:27
  • 17
    @Ryan: `git help ` is the best way to get documentation (and, it's actually pretty good). In this case, `git help stash` clearly show that `git stash clear` does the deed (this didn't stop me from coming to SO first and upvoting the answer... but that tells you more about me than `git` :D ) – Marco Massenzio May 31 '18 at 21:24
  • extending @kamal comments, if using `git stash list` simply moves the cursor to the next line without any output then you can be assured that all stashes are removed. – iCantC Mar 12 '20 at 11:55
148

This command enables you to look all stashed changes:

git stash list

Here is the following command use it to clear all of your stashed changes:

git stash clear

Now if you want to delete one of the stashed changes from stash area:

git stash drop stash@{index} # Index will be shown after getting stash list

Note: git stash list enables you to get index from stash area of git.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • 3
    Worth mentioning that at least for the integrated terminal in VSCode using Windows 10, it is `git stash drop 'stash@{index}'`, with apostrophes. – Oriol Miró Jan 27 '20 at 15:20
94

There are two ways to delete a stash:

  1. If you no longer need a particular stash, you can delete it with: $ git stash drop <stash_id>.
  2. You can delete all of your stashes from the repo with: $ git stash clear.

Use both of them with caution, it maybe is difficult to revert the once deleted stashes.

Here is the reference article.

Nesha Zoric
  • 6,218
  • 42
  • 34
  • 3
    Closing the terminal has nothing to do with it. git isn't a daemon; it only runs when you call it. It has no idea you closed the terminal. – DylanYoung Jan 09 '19 at 13:28
  • re: closing the terminal. I noticed there is a diagnostic message that prints: Dropped stash@{1} (hashid) and perhaps that can be useful to recover the stash. – Paul Aug 23 '22 at 13:10
42

I wanted to keep a few recent stashes, but delete everything else.

Because all stashes get renumbered when you drop one, this is actually easy to do with while. To delete all stashes older than stash@{19}:

while git stash drop 'stash@{20}'; do true; done
warp
  • 1,508
  • 15
  • 21
  • Does this apply to indexed values introduced in Git 2.11, where you can refer to the 3rd stash from the stash list using `git stash pop 3` instead? I wanted to know if we can use the `while` loop and drop stashes using index values. – tom_mai78101 Oct 14 '19 at 14:35
  • 1
    I love the simplicity of this, nice elegant solution! – dominic Apr 17 '20 at 06:12
  • Can I do something to keep some stashes in between the provided list. Like in your case it will delete all after 19. So for e.g. in btw 19 and last stash I have 2,3 stashes like at 23,25 that I need to keep. – Ammar Mujeeb Jul 31 '22 at 09:33
  • Or I can provide a range like stash indexes 20-30. @warp – Ammar Mujeeb Jul 31 '22 at 09:34
  • Thanks! this is what i was looking for – João Nunes Jun 14 '23 at 09:09
33

Clear all stashes at once

    git stash clear

List all stashes

    git stash list

delete specific stash

    git stash drop stash@{index}
Vikas Kumar
  • 339
  • 3
  • 4
21

To delete all stashes older than 40 days, use:

git reflog expire --expire-unreachable=40.days refs/stash

Add --dry-run to see which stashes are deleted.

See https://stackoverflow.com/a/44829516/946850 for an explanation and much more detail.

krlmlr
  • 25,056
  • 14
  • 120
  • 217
11

if you want to remove the latest stash or at any particular index -

git stash drop type_your_index

> git stash list

  stash@{0}: abc
  stash@{1}: xyz
  stash@{1}: pqr

> git stash drop 0

  Dropped refs/stash@{0}

> git stash list

  stash@{0}: xyz
  stash@{1}: pqr

if you want to remove all the stash at once -

> git stash clear
>

> git stash list
>

Warning : Once done you can not revert back your stash

Shivam Bharadwaj
  • 1,864
  • 21
  • 23
7

If you hit “git stash list” you will get the list of shashes.

enter image description here

Remove the stashes:

1)Delete only some selected stash, then run the below command:

git stash drop stash@{index}

2)Delete the complete list in one go, run the following command:

  git stash clear
Jagroop
  • 1,777
  • 1
  • 6
  • 20
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79
6

I had another requirement like only few stash have to be removed, below code would be helpful in that case.

#!/bin/sh
for i in `seq 5 8`
do
   git stash drop stash@{$i}
done

/* will delete from 5 to 8 index*/

Bala Krishnan
  • 550
  • 7
  • 11
  • 2
    try `for iterator in \`seq 5 8\`; git stash drop stash@{$iterator}; done` – Chris McCowan Apr 22 '19 at 20:15
  • 8
    Note that when you drop, the remaining stashes move up 1 spot. So if you want to remove from 5 to 8, you need to either count backwards or remove stash@{5} four times. For example: `for i in \`seq 4\`; do git stash drop 'stash@{5}'; done` – warp Aug 21 '19 at 13:36
6

If you just want to clear the latest stash or even you created a stash with git stash create then you should simply use

git stash drop

but if you want to clear all the git stashes of the current git repository then you can use

git stash clear

WARNING: Those states will then be subject to pruning(knock off), and maybe impossible to recover

Sujeet Mishra
  • 199
  • 3
  • 2
3

If you are working with VSCode, the 1.64 (Jan. 2022) will include git stash clear in its commands.

See issue 123375 and commit 302c41c:

https://user-images.githubusercontent.com/26203420/117556727-437aa600-b06c-11eb-9db6-ed80896b12f6.png

"command": "git.stashDropAll",
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Clear all stash from the list:

git stash clear

Clear particular index from the list:

git stash drop index
Jagroop
  • 1,777
  • 1
  • 6
  • 20