Skip to content

Instantly share code, notes, and snippets.

@y56
Created July 16, 2020 08:52
Show Gist options
  • Save y56/92547e541f2ea51f06858d189404096c to your computer and use it in GitHub Desktop.
Save y56/92547e541f2ea51f06858d189404096c to your computer and use it in GitHub Desktop.
git stash clear -- How can I delete all of my Git stashes at once?
How can I delete all of my Git stashes at once?
https://stackoverflow.com/questions/11369375/how-can-i-delete-all-of-my-git-stashes-at-once
Stack Overflow
Products
Customers
Use cases
Log in Sign up
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Home
Public
Stack Overflow
Tags
Users
Find a Job
Jobs
Companies
Teams
What’s this?
Free 30 Day Trial
How can I delete all of my Git stashes at once?
Ask Question
Asked 8 years ago
Active 1 month ago
Viewed 301k times
1428
How can I delete all of my Git stashes at once?
Specifically I mean, with typing in one command.
git
share improve this question
edited Oct 29 '13 at 20:56
Peter Mortensen
26.3k2121 gold badges9191 silver badges121121 bronze badges
asked Jul 6 '12 at 20:38
Rebekah Waterbury
15.8k55 gold badges2020 silver badges2626 bronze badges
add a comment
7 Answers
Active
Oldest
Votes
2495
1
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 (...).
share improve this answer
edited Jun 20 at 9:12
Community♦
111 silver badge
answered Jul 6 '12 at 20:41
Tadeck
108k2222 gold badges135135 silver badges182182 bronze badges
14
why not git stash drop ? – user20358 Sep 2 '15 at 11:48
139
@user20358: Because stash drop (" Remove a single stashed state from the stash list. When no <stash> 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 2 '15 at 15:48
2
@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 2 '16 at 1:36
13
In addition to what accepted answer mentioned - git stash clear, to confirm if all cleared, use - git stash list – kamal Sep 20 '17 at 8:15
11
@Ryan: git help <command> 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
show 2 more comments
49
There are two ways to delete a stash:
If you no longer need a particular stash, you can delete it with: $ git stash drop <stash_id>.
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.
share improve this answer
edited Jan 9 '19 at 18:59
answered May 25 '18 at 7:47
Nesha Zoric
3,5962828 silver badges3030 bronze badges
2
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 9 '19 at 13:28
add a comment
34
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} // here index will be shown after getting stash list.
Note : git stash list enables you to get index from stash area of git.
share improve this answer
edited Mar 27 '19 at 13:05
ibenjelloun
4,96511 gold badge1414 silver badges3939 bronze badges
answered Feb 27 '19 at 8:40
Vikrant Kashyap
4,56711 gold badge2222 silver badges4343 bronze badges
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 at 15:20
add a comment
13
1
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
share improve this answer
answered Aug 21 '19 at 13:40
warp
1,1601010 silver badges1919 bronze badges
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
I love the simplicity of this, nice elegant solution! – dominic Apr 17 at 6:12
add a comment
4
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
share improve this answer
edited May 18 at 3:57
answered Apr 7 at 8:09
Shivam Bharadwaj
79377 silver badges1515 bronze badges
add a comment
3
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*/
share improve this answer
answered Mar 29 '19 at 12:14
Bala Krishnan
34022 silver badges77 bronze badges
try for iterator in `seq 5 8`; git stash drop stash@{$iterator}; done – Chris McCowan Apr 22 '19 at 20:15
3
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
add a comment
2
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.
share improve this answer
answered May 23 at 2:55
community wiki
krlmlr
add a comment
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Name
Email
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged git or ask your own question.
The Overflow Blog
Tales from documentation: Write for your clueless users
Podcast 252: a conversation on diversity and representation
Upcoming Events
2020 Community Moderator Election
ends in 5 days
Featured on Meta
Feedback post: New moderator reinstatement and appeal process revisions
The new moderator agreement is now live for moderators to accept across the…
Allow bountied questions to be closed by regular users
2020 Community Moderator Election - Questionnaire
Linked
29
New repo with copied history of only currently tracked files
3
Delete stashed changes older than X days
Related
Hot Network Questions
What is the difference between "downloaden", "runterladen" and "herunterladen"?
What uniform are these gentlemen wearing?
Is it acceptable, as a Reviewer, to ask the Editor about another reviewer?
Is there a way to spread a list like in Typescript or Golang?
Mathematical Proof of a Rubik’s Cube
Do nuclei emit photons?
Colours in OpenVDB volumes
Which two numbers between 1 and 10 inclusive are the most difficult to distinguish when lip-reading?
How do you determine what action belongs to who in a modernist GM-based game?
How can I Bevel this sphere correctly?
Why didn't Lisa Simpson become an atheist?
How can I easily keep track of my landing count during pattern work?
Laguerre Polynomials
Can conflicting descriptions of the recent past be handled better than GM fiat?
National supercomputers
Is revealing the phone number during OTP verification process considered a vulnerability?
Are there any melee alternatives to Power Word Kill?
How should I respond to a potential employer thanking me for my application and telling me they'll be in contact?
How can I prove that 3 planes are arranged in a triangle-like shape without calculating their intersection lines?
Cleaning black stains from redwood
Always feeling behind in life in terms of money saved
Why does 'thoughts' and 'memories' not part of aggregates?
Is there really any point to go to Duna without a contract for Duna?
Can an outlet on a three-way circuit be made live full-time?
Question feed
Stack Overflow
Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness
Products
Teams
Talent
Advertising
Enterprise
Company
About
Press
Work Here
Legal
Privacy Policy
Contact Us
Stack Exchange
Network
Technology
Life / Arts
Culture / Recreation
Science
Other
Blog
Facebook
Twitter
LinkedIn
Instagram
site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2020.7.14.37215
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment