3

How do I reset git squash?

I am using PhpStorm. I squashed commits from last 14 days of work. The commit "HEAD DEV Refactor gulp file" has 50+ squashed commits inside.

How do I show / unsquash them again?

enter image description here

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Michal
  • 4,952
  • 8
  • 30
  • 63

1 Answers1

15

There is no "unsquashing" in Git normally. I am not familiar with your tool, so my answer applies for the Git command line. If you're on Windows, you probably have Git bash installed, and the tool may have a direct way of launching the Git commandline.

Make a backup of your state to prevent getting into worse trouble:

git tag unsquash-backup

First check if ORIG_HEAD is set to something you like. This should be the last state you had before a rebase.

git show ORIG_HEAD

If you see that it is the state you want to go back to, just do

git reset --hard ORIG_HEAD
git rebase --abort

If ORIG_HEAD is no longer what you want, you should use git reflog, which is the usual way of getting yourself out of trouble in Git if you've done something you shouldn't have.

git reflog

Then look for an old commit (which will look something like HEAD@{8}) in the list. That should be the state you want to go back to. Check that commit's history first to be sure by

git log HEAD@{8}

And then you can reset your branch to that state.

git reset --hard HEAD@{8}

Of course substituting HEAD@{8} everywhere with the commit you found in git reflog.

Note: standard warning applies, if you use git reset --hard, you lose any current uncommitted changes.

DUman
  • 2,560
  • 13
  • 16
  • Wow :) but git reflog only shows HEAD@ up to HEAD@{41} when I look at the commits I need HEAD@{49} is it possible to show more? – Michal Nov 01 '16 at 20:58
  • How far back the reflog goes cannot be said for certain, it's sometimes cleaned up by Git. `git reflog` shows everything, so if you scrolled all the way down and only saw up to 41, then that's it. You could also try `git fsck --unreachable` to see if it finds your commit. Did you try with ORIG_HEAD first? It should work if you haven't done anything after your squash. – DUman Nov 01 '16 at 21:03
  • git origin head shows the squashed commit. I will try that first. – Michal Nov 01 '16 at 21:08
  • `git reset --hard ORIG_HEAD` didn't do anything – Michal Nov 01 '16 at 21:10
  • I probably did it. Thanks a lot. I will accept your answer in an hour hopefully. – Michal Nov 01 '16 at 21:13
  • If something worked for you, please specify what in a comment or an edit to your question. – DUman Nov 01 '16 at 21:17
  • I will edit my answer when I am done fixing my mistakes. `git reflog` worked I just also learned tons of new things about GIT which is great. – Michal Nov 01 '16 at 21:21
  • great tip with `reflog` and using HEAD@{#number}, it saved my life :) – Extazystas Oct 15 '18 at 16:17