yourknightmares
yourknightmares

Reputation: 323

How to recover a commit in git

I worked on something all day in a personal project, and committed it to a branch. I then realized I was going to commit to master and created a new branch to push to, and git switched to that branch. Afterwards I switched back but couldn't find my commit. Is there any way to recover the commit from before I created the branch? It reverted a lot of my work.

Upvotes: 0

Views: 90

Answers (3)

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10064

If you remember the message (ctrl-r might help), you cand find the commit with:

git log --all --grep 'the message'

Upvotes: 1

DarkSigma
DarkSigma

Reputation: 416

The reflog command may be useful here. It will show the history of where the branch tip has been.

I'm not sure how you might have "lost" a commit if you truly did commit the work and are now checked out on that branch.

Upvotes: 1

dave
dave

Reputation: 64657

git reflog should help, as long as it was committed at some point it will still be there.

You'll get output like the following:

57a5b3a (HEAD -> some-branch, origin/some-branch) HEAD@{0}: commit: made changes
3092bca HEAD@{1}: commit: Other changes
7021685 HEAD@{2}: commit: More changes
2d882b8 HEAD@{3}: commit: even more changes
dc30cbc (origin/master, master) HEAD@{4}: checkout: moving from master to some-branch
dc30cbc (origin/master, master) HEAD@{5}: commit: I hope your commit messages are better than this

Just find the one that looks like the one you want and check it out

Upvotes: 5

Related Questions