Patrick Auld
Patrick Auld

Reputation: 1427

Recover git commits to (no branch)

I made a commit while on the (no branch) branch then did a pull, realized I was on the wrong branch and did a 'checkout master' and another pull. I now can't find my original commit to push on the master branch and I can't switch to the (no branch). Is there a way to resurrect my commit or get the diff?

Upvotes: 3

Views: 438

Answers (1)

Adrian Cornish
Adrian Cornish

Reputation: 23876

The git reflog will list for you all the commits you did, and a (for instance) git merge HEAD@{1} will merge it back into your branch.

$ git reflog
734713b... HEAD@{0}: commit: fixed refs handling, added gc auto, updated
d921970... HEAD@{1}: merge phedders/rdocs: Merge made by recursive.
1c002dd... HEAD@{2}: commit: added some blame and merge stuff

From git rev-parse:

<refname>@{<n>}, e.g. master@{1}

A ref followed by the suffix @ with an ordinal specification enclosed in a brace pair (e.g. {1}, {15}) specifies the n-th prior value of that ref.

Note that a git rebase -i would do the same.

The Revision Selection page mentions:

It’s important to note that the reflog information is strictly local — it’s a log of what you’ve done in your repository.
The references won’t be the same on someone else’s copy of the repository; and right after you initially clone a repository, you'll have an empty reflog, as no activity has occurred yet in your repository.

The config gc.reflogExpire specifies when to removes reflog entries older than this time; defaults to 90 days.

Upvotes: 6

Related Questions