DEV Community

Nick Vahalik
Nick Vahalik

Posted on • Edited on

How to (almost) lose data with git rebase

Was working on a feature just now and noticed that I had forgotten to add a service that I thought was committed 2 commits ago. No problem, right? just git rebase -i that sucker and put them into that commit!

$ git rebase -i <three refs ago>

OK. So I want to edit the commit from 2 refs ago and leave the last commit alone.

pick: <last commit>
edit: <target commit>

I save... and now I can go in and add my files.

git add src/app/services/
git commit --amend

It is at this point that I want to note that I actually have been playing around with using zsh's git plugin, which offers two aliases that are relevant to rebases:

  • grba: git rebase --abort
  • grbc: git rebase --continue

Also, what ended up happening at this point was this:

$ grba

Well, heck.

My commit was aborted, and those files that were amended to the rebased commit? Gone.

Thankfully PHPStorm's local history saved my code. Although in this particular case the service was all of 10 lines... but next time I shall have to be more careful when doing interactive rebases!

Update: See in comments below. The reflog held the data! It was not gone...

Top comments (3)

Collapse
 
jessekphillips profile image
Jesse Phillips • Edited

$ git reflog

Should show you each rebase commit and annotate it (rebase).

Also

$ git add src/app/services/
$ git commit --fixup <target commit>
$ git rebase <three refs ago> --autosquash
Collapse
 
nvahalik profile image
Nick Vahalik

I completely forgot about the reflog!

83b93b01 HEAD@{9}: rebase -i (abort): updating HEAD
54c6356c HEAD@{10}: commit (amend): TKT-181 - Add in service.
61472e94 HEAD@{11}: rebase -i: fast-forward
d87ad06c HEAD@{12}: rebase -i (start): checkout d87ad06c1365ef

You're right, the files I added are there:

git show --stat of that commit shows the files are indeed there.

Thank you!

Collapse
 
jessekphillips profile image
Jesse Phillips

Yeah, when you make a commit it is really hard to lose your work with Git. Know where to look or that something is missing can be hard though.