4

This may be hard to believe, but I was in the middle of executing a commit when my power went off. Luckily, I never pushed the commit. I just got my power back and when I tried to execute git log in the directory and I got the error:

fatal: bad default revision 'HEAD'

This is a private repository that I had been working on for a little over a week and have made about 5 - 10 commits.

I looked on SO and found Git tracking entire home directory. Get error - fatal: bad default revision 'HEAD', which was not of any help because I have already run a git init!

I just ran ls -la to see if my .gitignore file was still there, but lo and behold it was gone. What should I do? I don't want to lose my previous commits history.

My knowledge of Git is limited. Any help would be appreciated.

Edit 1:

I only have one branch (master) and when I tried: git symbolic-ref HEAD refs/heads/master it didn't change anything.

I also looked at git log and show on a bare repo and tried:

git log --graph --oneline --date-order --decorate --color --all

which gave me:

095cdaa (origin/master) added profile layout
dd9ddec fixed nav bar
4e99ca7 remove unecessary files, add resources, start navigation drawer
8b09709 fix README
c64c422 initial commit

I then tried git branch master 095cdaa..., which gave me the error:

fatal: Failed to resolve HEAD as a valid ref.

Edit 2:

As a quick fix to this problem, I did what @PaulGriffiths suggested:

  1. Clone the repository
  2. Copy the changed files and commit them
  3. Run rm -f ./.git/index.lock
Community
  • 1
  • 1
lschlessinger
  • 1,934
  • 3
  • 25
  • 47
  • If you say that you "never pushed the commit", presumably this means you have some kind of remote repository that contains all your previous commits? – Crowman May 16 '14 at 21:53
  • 1
    git fsck is the command you want, I think. – bmargulies May 16 '14 at 21:54
  • This question can be improved by giving it a more descriptive title. Currently the title is very vague. –  May 16 '14 at 21:55
  • @PaulGriffiths Yes, I have a private remote repo on GitHub that has about 5-10 commits. – lschlessinger May 16 '14 at 21:57
  • 2
    @lschessinger: Is there therefore any reason why you can't just clone that repository again into a new directory? If the actual files you have currently are good, you should be able to just copy the changed files in and commit them, and end up exactly where you wanted to be. – Crowman May 16 '14 at 22:01
  • @bmargulies just tried that. I got this error: `error: Invalid HEAD` error: unable to unpack 05908e7.... header` `error: inflateEnd: stream consistency error (no message)` `fatal: looses object 05908e7.... (stored in .git/objects/05/908e7....) is corrupt` – lschlessinger May 16 '14 at 22:01
  • @PaulGriffiths No, that would work, but I would have to copy a lot of files. I was just wondering if there was another quick fix. – lschlessinger May 16 '14 at 22:11
  • 1
    @lschessinger: Well, you could just copy all the files in one go, and when you `git commit -a`, it'll only actually commit the ones that are different from the current versions in the remote repo. Giving you an out when something bad happens to your local repository is the primary benefit of pushing to a remote one for a single-person project. – Crowman May 16 '14 at 22:13
  • @Cupcake I just changed it. – lschlessinger May 16 '14 at 22:54
  • @lschessinger: Two possibilities: (1) Did you rename the original directory? If not, you might just be cloning into the existing one, so rename it first, and then make your new clone. (2) By "copy all the files", that does not include the `.git` directory in your original folder, make sure you don't copy that. – Crowman May 17 '14 at 02:44
  • @PaulGriffiths I did that. Sorry for deleting the comment. I actually had to run `rm -f ./.git/index.lock` for it to work. – lschlessinger May 17 '14 at 02:48
  • Would http://stackoverflow.com/a/15440424/6309 help? – VonC May 17 '14 at 08:12

2 Answers2

0

Have a look at : Git internals - Maintenance and Data Recovey in the official git book.

If your reflog is still intact :

git reflog

will show a list of the last states of the HEAD commit. Try running git checkout <hash> on one of the listed commits, then git branch <newBranchName>.

LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

As suggested by @PaulGriffiths, I fixed this issue by:

  1. Cloning the repository.
  2. Copying the changed files and committing them.
  3. Running rm -f ./.git/index.lock in the repository.

This solution only worked because I had a remote repository to which I had made many commits and pushed them before. If I had never pushed the commits, this wouldn't work.

lschlessinger
  • 1,934
  • 3
  • 25
  • 47