Get a list of all git commits, including the 'lost' ones
Solution 1:
Try:
git log --reflog
which lists all git commits by pretending that all objects mentioned by reflogs (git reflog
) are listed on the command line as <commit>
.
Solution 2:
Not particularly easily- if you've lost the pointer to the tip of a branch, it's rather like finding a needle in a haystack. You can find all the commits that don't appear to be referenced any more- git fsck --unreachable
will do this for you- but that will include commits that you threw away after a git commit --amend
, old commits on branches that you rebased etc etc. So seeing all these commits at once is quite likely far too much information to wade through.
So the flippant answer is, don't lose track of things you're interested in. More seriously, the reflogs will hold references to all the commits you've used for the last 60 days or so by default. More importantly, they will give some context about what those commits are.
Solution 3:
When I tackle this issue I use the following command:
git reflog | awk '{ print $1 }' | xargs gitk
This lets me visualise recent commits which have become headless.
I have this wrapped up in a script helper called ~/bin/git-reflog-gitk
.
Solution 4:
What saved my life was the following command:
git reflog
There you find a screen with history commits done to git like this one:
At this point, you only have to find the HEAD@{X}
that you need, create a temporary branch and move to it like this:
git checkout -b temp_branch HEAD@{X}
That way you will have a temporary branch with your lost commit without rebasing or breaking even more your git repository.
Hope this helps...
Solution 5:
Like @Kieran 's Answer, but for the console:
git log --oneline --all --graph --decorate $(git reflog | awk '{print $1}')