Rebekah Waterbury
Rebekah Waterbury

Reputation: 23796

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

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

Specifically I mean, with typing in one command.

Upvotes: 2213

Views: 774221

Answers (12)

Vikrant Kashyap
Vikrant Kashyap

Reputation: 6846

This command enables you to look all stashed changes:

git stash list

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

git stash clear

Now, if you want to delete a specific one from the stashed changes in 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.

Upvotes: 187

Shubham Verma
Shubham Verma

Reputation: 9933

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

Upvotes: 20

Vivek Bhandari
Vivek Bhandari

Reputation: 201

Clear all stash from the list:

git stash clear

Clear particular index from the list:

git stash drop index

Upvotes: 10

VonC
VonC

Reputation: 1323973

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",

Upvotes: 3

Vikas Kumar
Vikas Kumar

Reputation: 349

Clear all stashes at once

    git stash clear

List all stashes

    git stash list

delete specific stash

    git stash drop stash@{index}

Upvotes: 34

Sujeet Mishra
Sujeet Mishra

Reputation: 209

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

Upvotes: 6

krlmlr
krlmlr

Reputation: 25444

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.

Upvotes: 24

Shivam Bharadwaj
Shivam Bharadwaj

Reputation: 2296

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

Upvotes: 11

warp
warp

Reputation: 1578

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

Upvotes: 48

Tadeck
Tadeck

Reputation: 137320

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 (...).

Upvotes: 3772

Bala Krishnan
Bala Krishnan

Reputation: 580

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*/

Upvotes: 6

Nesha Zoric
Nesha Zoric

Reputation: 6620

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.

Upvotes: 99

Related Questions