randouser
randouser

Reputation: 109

How to recover missing git commits?

I was playing around with a project in git and I appear to have lost months of commits.

Here is what I did:

git checkout veryoldcommit

I looked around in the files, found what I needed (didn't make any changes).

I then ran git checkout master to take me back to the latest commit.

I then wanted to delete only the very latest commit, so following advice here I ran git checkout HEAD-.

This threw an error, I made a typo. I include here in case it is relevant.

I then ran git checkout HEAD~ (with a tilde this time).

Now, I am stuck at veryoldcommit and I can't find how to access the newer commits. I'm terrified to touch anything 'cause I'm afraid I'll mess things up more.

I have months of work in that project, and it is not backed up to a remote repository.

What have I done? And, is it fixable?

Upvotes: 2

Views: 57

Answers (1)

JD Frias
JD Frias

Reputation: 4686

Yes it's fixable, that's the nice thing about git it doesn't destroy anything. Try this:

git log --graph --all --format='%h %s%n        (%an, %ar)%d' --abbrev-commit --reflog  

And look for the commit hash you want. You can create a new branch based on that commit with:

git checkout -b my-branch XXXXXX

or reset the current branch to point to that commit

git reset --hard XXXXXXX

good luck.

If you're paranoid make a copy of your .git directory somewhere else before doing this.

Upvotes: 1

Related Questions