1

I think I just totally messed up.

I've been working on a project for 2 days and I was trying Magit in emacs to commit the project but I kept getting back to the staging area without finishing the commit.

Long story short, I exited emacs and was back in the command line and everything was fine with the files, but git status said that I should do a git revert --abort or some other option I don't recall now.

Well, git revert --abort seemed ok, since I wanted to abort whatever git was doing and being left with my files, but it actually took all my files to the last commit.

I've lost hours upon hours of work. Is there any way to go back to the state before this command was issued?

My git reflog is:

5fba267 HEAD@{0}: commit: Clones the spacemacs repo needed for emacs
2c9ced3 HEAD@{1}: commit: Major changes to emacs configuration
d38f9db HEAD@{2}: commit: add initial emacs configuration and vimwiki
e0e28d0 HEAD@{3}: commit: Vim whitespace showing modifications

The work I did was all after that 5fba267 HEAD@{0}


EDIT with answer: Using the invaluable help from codeWizard I managed to do it in the end.

Using the dangling blob didn't work so much because there were almost no files there. This might be because while trying to solve it I changed one of the files back to the last checkout and git deleted the other files from the dangling stuff.

But, all the information needed is actually in the dangling commit part.

So here is what I did with the help of codeWizard

First, copy your repository directory in full and only work from there.

Next issue the git command:

git fsck --full

It outputs something like:

Checking object directories: 100% (256/256), done.
dangling blob 0c8676d28b6b063592493bce281530fb7098a41c
dangling blob 5040b2db1a895ea4aa69c993e0df7cd48823eaec
dangling blob 5ffc6f27406506a01aa13e9b773b574840cef7ac
dangling blob 676efa400477c8d92787a39978f73114f39e0f89
dangling commit 8a44f14eb0821859c0e1cdf0d81038c8431a1007
dangling commit 968e27b0d49b899ab2d43a964b561e4dfd2aa41f
dangling commit 9f5e56caa8b904d93acd43b05f3817ff975a0423

Now, showing the dangling commit using the SHA:

git show - p 8a44f14eb0821859c0e1cdf0d81038c8431a1007

The output will actually be the git diff for that staged but never committed changes:

commit 8a44f14eb0821859c0e1cdf0d81038c8431a1007
Author: Me <me@gmail.com>
Date:   Sun Jan 3 02:07:10 2016 +0000

    oO

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ec6eec8
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+/#bootstrap.sh#
+/emacs.d/#init.el#
+*~
diff --git a/bootstrap.sh b/bootstrap.sh

What you need to do it to check the Date and see the one that suits you (actually they where all from the same period for me).

After that you just have to issue the command: git checkout 8a44f14eb0821859c0e1cdf0d81038c8431a1007

And you will be will all your files added to commit but that you've never committed.

Only issue I've found was that after that you will be left with the git status:

HEAD detached at 8a44f14 
nothing to commit, working directory clean

So I just copied all the changed files back to my original repo and everything was fine.

jbssm
  • 6,861
  • 13
  • 54
  • 81
  • What does `git reflog` say? Can you add that to your question so that we can see what Git's last few commands were around your `git revert --abort` operation? – Makoto Jan 03 '16 at 02:31
  • Thank you. I've added it now. It only logs my last push, there is nothing from the work I did since then. – jbssm Jan 03 '16 at 02:37
  • Maybe I misread it. All of the work that you lost was unstaged? – Makoto Jan 03 '16 at 02:39
  • It was staged but not committed. Since I couldn't figure out out to commit it in magit, I went back to the command line and `git status`gave me the message to do a `git revert --abort`. After that, I went back to my last commit. – jbssm Jan 03 '16 at 02:43
  • Possible duplicate of [In Git, how can I recover a staged file that was reverted prior to committing.](http://stackoverflow.com/questions/11094968/in-git-how-can-i-recover-a-staged-file-that-was-reverted-prior-to-committing) – Makoto Jan 03 '16 at 02:45
  • Hi, that question is indeed similar, but it seems to work only for a single file. For many changed files, the answers in that question just don't work. I actually found a solution for this problem with the help of CodeWizard and I've posted it. It also has the added benefit that unlike that question you refer to, the method is automated by git itself. – jbssm Jan 03 '16 at 13:17

1 Answers1

0

First of all make a backup of the whole folder to start with.

Secondary : Yes you can if you have committed your files?

Did you commit or at least git add them?

If you did ill explain how you can find them and get them back,

if you did not at least add them to the index then you lost them.
Update me and ill update my answer accordingly.


How to recover deleted files (assuming they are committed)

The trick is to checkout the previous commit of the last good commit (this should be your revert commit in your question)

Then checkout the version at the commit before, using the caret (^) symbol:

git checkout <last good commit>^ 

The trick is the ^ which stands for the previous commit


I also did the git add and they were in the staging area ready to commit, but I've never passed the commit part.

Here is a sample on how to lose my file enter image description here

So you are saved (but still you have some work in front of you.)

  1. Backup the folder (as you already did)
  2. execute git fsck --full

How to recover my lost file

enter image description here

Execute git fsck --full and then loop over the files are check to see which one is yours using the git cat-file -p <SHA>

As you can see in the sample image i managed to recover my un-commitd file b.txt

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    Thank you. I did the copy already. I also did the `git add` and they were in the staging area ready to commit, but I've never passed the commit part. – jbssm Jan 03 '16 at 02:48
  • Good so you can save them :-) Updating my answer – CodeWizard Jan 03 '16 at 02:53
  • So, just to be sure, I should do `git checkout 5fba267^` ? – jbssm Jan 03 '16 at 03:00
  • Follow the steps i posted in my answer, ill keep updating ti according to your inputs – CodeWizard Jan 03 '16 at 03:04
  • Follow the 2 images i posted to show you how i "lose" file and how do i "recover" it – CodeWizard Jan 03 '16 at 03:05
  • I'm doing that. If I do it to a dangling commit, I get: `tree 4dbc07d09a6fefbf531181178357de2c9f84a81f parent 5fba2679aa9b1e234a262e60ab0a03ae8ced217e author Me 1451786814 +0000 committer Me 1451786814 +0000` If I do it to a dangling blob, I get parts of files. – jbssm Jan 03 '16 at 03:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99565/discussion-between-codewizard-and-jbssm). – CodeWizard Jan 03 '16 at 03:10