Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discard Git Stash Pop

Tags:

git

I did a git stash pop and now I have a ton of conflicts. I had committed all my recent code before the git stash pop, so is there a way to go back to the last commit and get rid of all the conflicts and code the git stash pop injected?

like image 696
jdog Avatar asked Nov 18 '13 00:11

jdog


People also ask

Does git stash pop delete?

Git stash is a temporary storage. When you're ready to continue where you left off, you can restore the saved state easily: git stash pop . Popping your stash removes the changes from your stash and reapplies the last saved state.

Can I undo a git stash?

To undo a git stash , use the git stash pop command. It will re-apply your stash to your working copy.

How do I pop a specific stash?

To pop a specific stash in git, you can use the git stash apply command followed by the stash@{NUMBER} command. command. It will show the list of stashes you have saved.


2 Answers

This has already been asked and answered on stackoverflow (see How to revert Git repository to a previous commit?), but the simple answer is:

git reset --hard HEAD

This should take care of your problem. Note that this removes all uncommitted changes from the repository.

Note that if there are conflicts, the stash is preserved. From the stash docs:

Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.

like image 157
MichaelMilom Avatar answered Oct 17 '22 10:10

MichaelMilom


Reset can also be called on specific files :

git reset HEAD <filename>...

You can't hard reset a file though. But you can revert the changes with checkout afterwards :

git checkout -- <filename>...

You stash will be preserved, as pointed out by Luke in MichaelMilom answer.

This is useful when you don't want to lose your un-committed local changes.

like image 6
Poppolopoppo Avatar answered Oct 17 '22 08:10

Poppolopoppo