How to delete a stash created with git stash create?

GitGit Stash

Git Problem Overview


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

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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.

Solution 6 - Git

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

Solution 7 - Git

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

Solution 8 - Git

It also works

git stash drop <index>

like

git stash drop 5

Solution 9 - Git

The Document is here (in Chinese)please click.

you can use

> git stash list

> git stash drop stash@{0}

enter image description here

Solution 10 - Git

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)

Solution 11 - Git

git stash list

git stash drop 1

enter image description here

Solution 12 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul WaglandView Question on Stackoverflow
Solution 1 - GitCB BaileyView Answer on Stackoverflow
Solution 2 - GitdahlbykView Answer on Stackoverflow
Solution 3 - GitFelipe PereiraView Answer on Stackoverflow
Solution 4 - GitJorgeView Answer on Stackoverflow
Solution 5 - GitShalom SamView Answer on Stackoverflow
Solution 6 - GitVinicius CardosoView Answer on Stackoverflow
Solution 7 - GitBilal YaqoobView Answer on Stackoverflow
Solution 8 - GitMonTeaView Answer on Stackoverflow
Solution 9 - GitLongaleiView Answer on Stackoverflow
Solution 10 - GitShubham JaiswalView Answer on Stackoverflow
Solution 11 - GitYasir TanveerView Answer on Stackoverflow
Solution 12 - GitShoaib KhalilView Answer on Stackoverflow