Marcus Hansson
Marcus Hansson

Reputation: 816

Restoring lost commits in git

I have a local repository, let's call that A.

In A I have lots of LOST_FOUND's that I want to restore. What would be the best (and least dangerous) way of restoring said commits?

(I can't really say why they're lost, I honestly thought I was doing a simple "commit, go home for the day"-kinda operation, but apparently it became something else.

EDIT: Doing a git branch foundit LOST_FOUND_N results in

error: Object #SHA1 is a blob, not a commit
fatal: Not a valid branch point: `LOST_FOUND_N`.

(Where #SHA1 is a, well, SHA1, and N is the number of the LOST_FOUND)

EDIT 2: Doing a git show on the LOST_FOUND_-files shows that they mostly map to the files that I've lost (but not all of them seem to belong to the same commit (as far as I can tell, I can't remember exactly what I've commited each time), or even the same branch).

Upvotes: 1

Views: 528

Answers (2)

John Lawrence Aspden
John Lawrence Aspden

Reputation: 17500

I've got the following alias in my .bashrc:

alias gitkall='git fsck --lost-found | awk "{ print \$3 ; }" | xargs gitk --all'

It makes a command gitkall, which shows every revision in the repository, including those which are left over from history edits etc.

From the gitk gui, it's easy and intuitive to tag interesting lost revisions, make new branches etc.

Upvotes: 4

Marcus Hansson
Marcus Hansson

Reputation: 816

I'll just go ahead and answer this myself:

Note that this answer is specific to this question, and not a universal solve-by-magick:

  1. Inspect the file with
    git show FILE
  2. When you recognize the file, pipe it like so:
    git show FILE > FILENAME

Do note that this only give you the contents of the files you find, not the structure of the directory.

Upvotes: 0

Related Questions