[git] How to delete a stash created with git stash create?

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

This question is related to git

The answer is


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.


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.


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.


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)


It also works

git stash drop <index>

like

git stash drop 5

The Document is here (in Chinese)please click.

you can use

git stash list

git stash drop stash@{0}

enter image description here


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

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

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