Discard Git Stash Pop

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

This has already been asked and answered on stackoverflow (see https://stackoverflow.com/questions/4114095/revert-to-previous-git-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.

Solution 2 - Git

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.

Solution 3 - Git

If you never want to see the work in the popped stash again, this is as simple as a hard reset:

git reset --hard HEAD

This tells git to ignore the fact that you have uncommitted changes in your working directory, and sets the working directory, the staging area, and head to the commit you specify -- in this case, the existing HEAD, that contains all the work you've just committed.

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
QuestionjdogView Question on Stackoverflow
Solution 1 - GitMichaelMilomView Answer on Stackoverflow
Solution 2 - GitPoppolopoppoView Answer on Stackoverflow
Solution 3 - GitjscsView Answer on Stackoverflow