939

Git stash seems to do a lot of what I want, except that it is a little hard to script, as the if you have no changes, then git stash; git stash pop will do something different than if you do have changes in your repository.

It appears that git stash create is the answer to that problem, and everything works, except for one thing… I can't get rid of the created stash. Is there any way to get rid of the stash?

To make it 100% clear what I am doing:

Create the stash:

~/tmp/a(master) $ git stash create 
60629375d0eb12348f9d31933dd348ad0f038435
~/tmp/a(master) $ git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   b
#
~/tmp/a(master) $ git reset --hard
HEAD is now at 555d572 log message

Use the stash:

~/tmp/a(master) $ git apply 60629375d0eb12348f9d31933dd348ad0f038435
fatal: can't open patch '60629375d0eb12348f9d31933dd348ad0f038435': No such file or directory
~/tmp/a(master) $ git stash apply 60629375d0eb12348f9d31933dd348ad0f038435
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   b
#

Delete the stash: (except that this last bit doesn't work)

~/tmp/a(master) $ git stash drop !$
git stash drop 60629375d0eb12348f9d31933dd348ad0f038435
'60629375d0eb12348f9d31933dd348ad0f038435' is not a stash reference
frederj
  • 1,483
  • 9
  • 20
Paul Wagland
  • 27,756
  • 10
  • 52
  • 74
  • Despite the distance in time, I want to point out for fellow travelers that the `git apply` run in the second code box at the time of this writing didn't apply a patch—the error message in the output told you this. So in fact, you tried to create a stash (which didn't work, see comments below) apply a stash that didn't get created then drop a stash that didn't get created. This is why nothing you did worked. – Professor Tom Mar 08 '18 at 22:04
  • 1
    @ProfessorTom the stash was created, that is the why it returned a hash code, and why the `git stash apply` did work. The `git apply` attempts to read a local patch file, which doesn’t exist. To be honest, it probably just shouldn’t be there, and it could never have worked. I’m vaguely tempted to remove it from the question, but given that it has helped so many in its current form, I will leave it as is. – Paul Wagland Mar 09 '18 at 06:30

13 Answers13

1296

git stash drop takes no parameter - which drops the top stash - or a stash reference which looks like: stash@{n} which n nominates which stash to drop. You can't pass a commit id to git stash drop.

git stash drop            # drop top hash, stash@{0}
git stash drop stash@{n}  # drop specific stash - see git stash list

Dropping a stash will change the stash@{n} designations of all stashes further down the stack.

I'm not sure why you think need to drop a stash because if you are using stash create a stash entry isn't created for your "stash" so there isn't anything to drop.

user2864740
  • 60,010
  • 15
  • 145
  • 220
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Well, the entry is created using "stash"… One would logically think that it would be removed using "stash". – Paul Wagland Apr 21 '11 at 05:37
  • 2
    Look at the man page for git stash. stash create doesn't create an entry, only the commit object for the stash so there is no entry in the stash log to remove. – CB Bailey Apr 21 '11 at 06:06
  • 3
    [From man](http://man.he.net/man1/git-stash): **create** Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace. – ruffin Jun 06 '12 at 14:13
  • 80
    **Caution**: You probably shouldn't drop multiple like 1, 2, 3, as you'll actually end up dropping what was originally numbers 1, 3, and 5. The correct way to drop 1, 2, and 3, would be to do them in the order 3, 2, 1, or 1, 1, 1. Also, it's 0 indexed, with 0 being at the top of the stack. – ArtOfWarfare Sep 17 '13 at 15:36
  • Is it possible with a single operation to completely clear a stash that contains multiple indexes ? – BaltoStar Jun 15 '14 at 23:10
  • 2
    Git Extensions issues an unknown command when the "Stash Changes" button is pressed, and whatever it does persists over exiting and restarting the application. There is no obvious way to get rid of the stash using the GUI, but from the command line "git stash list" shows the WIP stash, and "git stash clear" gets rid of it. – Dave Mar 16 '16 at 19:27
  • 3
    If you're doing this from in powershell make sure you wrap stash@{n} in single quotes otherwise you get an error. – Brian Surowiec Sep 12 '16 at 21:28
  • With modern versions of Git you can also do `git stash drop 2`, `git stash show ` etc – FreelanceConsultant Jan 10 '23 at 15:43
607

To delete a normal stash created with git stash , you want git stash drop or git stash drop stash@{n}. See below for more details.


You don't need to delete a stash created with git stash create. From the docs:

Create a stash entry (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace. This is intended to be useful for scripts. It is probably not the command you want to use; see "save" above.

Since nothing references the stash commit, it will get garbage collected eventually.


A stash created with git stash or git stash save is saved to refs/stash, and can be deleted with git stash drop. As with all Git objects, the actual stash contents aren't deleted from your computer until a gc prunes those objects after they expire (default is 2 weeks later).

Older stashes are saved in the refs/stash reflog (try cat .git/logs/refs/stash), and can be deleted with git stash drop stash@{n}, where n is the number shown by git stash list.

dahlbyk
  • 75,175
  • 8
  • 100
  • 122
  • 25
    It doesn't answer the titular question. As a result, people like me, who arrive at the question via Google searching for the answer to the titular question and don't particularly care for the actual details, up vote the person who actually answered it. Some people also spitefully down vote the accepted answer because it doesn't help them. I personally just upvoted the answer that helped me. – ArtOfWarfare Sep 17 '13 at 15:33
  • 17
    So: 1) Google "git delete stash" 2) click SO link based on first half of question title 3) downvote correct answer specific to the second half of the title. That's a new one. – dahlbyk Sep 18 '13 at 03:19
  • 12
    @ArtOfWarfare That would be a lack of reading comprehension then, because this is very clearly a response to the "titular question". – Chris Hayes Sep 18 '13 at 03:24
  • 1
    Fair enough. I create my stashes using just `git stash` - I don't know if that actually maps to `git stash create` or something else. Git is certainly something I'm still learning about (if it wasn't, I wouldn't be reading SO Q&As like this.) – ArtOfWarfare Sep 18 '13 at 10:36
  • I here looking fr how drop a git stash by commit-id as dropping a stash by relative order (eg stash@{3}) is race condition:position of stash may have moved between when located and when time to delete it.Have a cron job that runs every 10 min that stash-creates work.If diff changes from last run, save it in timestamped patch file,then it stash-store it and remove the stash from the previous cron job.If I interactively stash something at the exact instant that the cron job is dropping stash@{6} or whatever, stash@{6} may not actually be the stash it intended to drop any more. – Adrian Pronk Apr 03 '16 at 10:06
  • 1
    @AdrianPronk This is exactly the sort of thing I would recommend actually using `git stash create` for. Rather than save in a timestamped patch file, you might just let the git reflog save it for you in a custom ref (e.g. `refs/backup`). I would try something like 1) `git stash create`, 2) compare new stash's tree with `refs/backup^{tree}`, 3) if the tree is different, `git update-ref --create-reflog refs/backup `. Eventually old backup stashes will be pruned automatically. – dahlbyk Jun 22 '16 at 01:07
  • @ArtOfWarfare For the first time ever, I upvoted all the answers (because was useful to me) except the selected-correct answer LOL. Can't agree more with you – evilReiko Oct 17 '17 at 08:33
  • git stash drop stash@{n} worked for me while deleting a particular stash. – Sachidananda Naik Jun 21 '21 at 04:18
268

If you are 100% sure that you have just one stash or you want to delete all stashes (make a git stash list to be 107% sure), you can do a:

git stash clear

..and forget about them (it deletes all stashes).

Note: Added this answer for those who ended up here looking for a way to clear them all (like me).

user2864740
  • 60,010
  • 15
  • 145
  • 220
Felipe Pereira
  • 11,410
  • 4
  • 41
  • 45
  • 33
    If you only have one stash, I would still recommend to use `git stash drop`, since that only drops one stash, and you don't have to worry about losing more than expected. – Paul Wagland Aug 31 '14 at 10:48
  • 1
    Upvoted as it actually answers the question, whether or not it's best practice to do so. I actually need this, as the stashes I have created today need to be gone. – Alex McCabe Oct 06 '14 at 11:21
  • 3
    I've been using stash for a long time now. This time however I wasn't able to get rid of the last stash I made by simply using `git stash drop` as usual. I am not sure of what I did differently. Anyway, `git stash clear` worked for me. Didn't know it existed. +1 – Konstantinos Gaitanis Jan 23 '15 at 12:39
  • 3
    +1 because git was refusing to drop the stash, since one of the files created by the stash already existed now.. – Thiago Barcala Mar 11 '15 at 14:41
87

From git doc: http://git-scm.com/docs/git-stash

drop [-q|--quiet] []

Remove a single stashed state from the stash list. When no is given, it removes the latest one. i.e. stash@{0}, otherwise must be a valid stash log reference of the form stash@{}.

example:

git stash drop stash@{5}

This would delete the stash entry 5. To see all the list of stashes:

git stash list
slm
  • 15,396
  • 12
  • 109
  • 124
Jorge
  • 1,353
  • 10
  • 25
  • 4
    Except that I explicitly say at the bottom of the question that git stash drop doesn't do what I want. – Paul Wagland Apr 22 '15 at 18:29
  • 1
    I would recommend checking content of a stash using - `git stash show stash@{REPLACE_IT_WITH_STASH_INDEX}` before executing `git stash drop`. Believe me, it will save a lot of trouble :) – realPK Oct 10 '16 at 04:53
73

You should be using

git stash save

and not

git stash create

because this creates a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace. Hence won't be accessible with stash apply.

Use git stash save "some comment" is used when you have unstaged changes you wanna replicate/move onto another branch

Use git stash apply stash@{0} (assuming your saved stash index is 0) when you want your saved(stashed) changes to reflect on your current branch

you can always use git stash list to check all you stash indexes

and use git stash drop stash@{0} (assuming your saved stash index is 0 and you wanna delete it) to delete a particular stash.

Shalom Sam
  • 1,539
  • 1
  • 16
  • 26
  • 3
    Just for information - Simply typing the command `git stash` defaults to `git stash save` – RBT Sep 13 '17 at 12:16
  • 1
    `git stash create` is intended to be used by scripts. That's OP's use case. So, recommending `git stash save` may be useful for "normal" uses, but not for OP's use case. OP may want to have a look at the [docs](https://git-scm.com/docs/git-stash): there are more commands related to scripting, for example [`git stash store`](https://git-scm.com/docs/git-stash#git-stash-store) – Adrian W Nov 29 '18 at 11:20
  • 1
    `git stash save "message"` nowadays should read `git stash push -m "message"`. Compare `git commit -m "message"` – Tino May 23 '19 at 12:35
47

To delete only one stash git stash drop

Delete all stashes git stash clear

To show your stash git stash show

To remove an specific stash git stash drop indexnumber e.g git stash drop 4

Vinicius Cardoso
  • 666
  • 8
  • 23
41

The best way to deal with stashes, first check the list of stash with

git stash list

and then identify and confirm if there's a stash of your concern then go with

git stash drop

and it will delete the stashes one by one (starts from top)

git stash drop <index>

it'll delete a stash on a specific index i.e 5

but if you're sure that nothing in that list in needed then go for

git stash clear

and it'll remove all of the them.

Shoaib Khalil
  • 1,874
  • 17
  • 10
37
git stash           // create stash,
git stash push -m "message" // create stash with msg,
git stash apply         // to apply stash,
git stash apply indexno // to apply  specific stash, 
git stash list          //list stash,
git stash drop indexno      //to delete stash,
git stash pop indexno,
git stash pop = stash drop + stash apply
git stash clear         //clear all your local stashed code
juhan_h
  • 3,965
  • 4
  • 29
  • 35
Bilal Yaqoob
  • 379
  • 3
  • 2
6

It also works

git stash drop <index>

like

git stash drop 5
MonTea
  • 1,166
  • 1
  • 13
  • 30
5

For VS Code users who want to operate the stash from UI itself, please use this method:

  1. Make sure you have installed GitLens extension.
  2. Go to Source Control tab.
  3. Check the STASHES section. You will find all your stashes (same like git stash list)
  4. You will find direct buttons to Apply Stash, Compare with HEAD and Delete Stash respectively for each stash.

enter image description here

  1. To drop a particular stash, click on Delete Stash button and give confirmation to delete (drop) it.

enter image description here

Akshay
  • 926
  • 9
  • 21
4

git stash list

git stash drop 1

enter image description here

3

The Document is here (in Chinese)please click.

you can use

git stash list

git stash drop stash@{0}

enter image description here

Longalei
  • 453
  • 3
  • 8
  • `git stash create` is different to `git stash save`, in particular, the create form doesn’t show up in list, and cannot be dropped, hence the question. – Paul Wagland Jul 26 '19 at 20:35
  • @PaulWagland Thanks your reply and supply the answer. Honestly,I'm not find the "git stash create" int the official document. But about this,It's interested me.if you want to find the "git stash create" in the "git stash list". you can do this above. – Longalei Jul 28 '19 at 05:55
3

Git stash clear will clear complete stash,

cmd: git stash clear

If you want to delete a particular stash with a stash index, you can use the drop.

cmd: git stash drop 4

(4 is stash id or stash index)