Get a list of all git commits, including the 'lost' ones

GitGit Log

Git Problem Overview


Let's say that I have a graph like this:

A---B---C---D (master)
     \
      \-E---F (HEAD)

If I do git log --all --oneline, I will get all six of my commits.

But if the graph is

A---B---C---D (master, HEAD)
     \
      \-E---F

I will not see E and F. Can I get git to tell me all the commits, including those on branches which are not named?

Thanks

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

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.

Edit: - my git-reflog-gitk wrapper is redundant - as you can now call gitk --reflog directly

Solution 4 - Git

What saved my life was the following command:

git reflog

There you find a screen with history commits done to git like this one:

enter image description here

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 - Git

Like @Kieran 's Answer, but for the console:

git log --oneline --all --graph --decorate  $(git reflog | awk '{print $1}')

Solution 6 - Git

git log --reflog

saved me! I lost mine while merging HEAD and could not find my lates commit! Not showing in source tree but git log --reflog show all my local commits before

Solution 7 - Git

How I solve this problem? Use git fsck and logging!

First create a file containing lost (unreachable) commits and blobs. (NOTE: if you did something like git gc then it will garbage collect all of they commits and you won't find them here!)

$git fsck --lost-found > lost_found.commits

That gives you a file like this: > dangling commit dec2c5e72a81ef06963397a49c4b068540fc0dc3
> dangling blob f8c2579e6cbfe022f08345fa7553feb08d60a975
> dangling blob 0eb3e86dc112332ceadf9bc826c49bd371acc194
> dangling blob 11cbd8eba79e01f4fd7f496b1750953146a09502
> dangling commit 18733e44097d2c7a800650cea442febc5344f9b3
> dangling blob 1e53a5cdb3ecdde27081ec6e8b31e4070106ee05

You can then open this file with you favorite text editor to copy the commit/blog hashes from there. (*cough* vim macros works great for this *cough*)

Now you can log back from this commit with something like git log --oneline <commit hash>. Alternatively, gitk, tig, or any other git viewer should work.

In your case if you find the hash for commit F the log will show you something like this,

A---B---E---F

Quick and easy! Now you can find the context behind all of those dangling commits.

P.S. Yes, I know, late post, but oh well, somebody might find it here and find it useful. (Mostly likely me in 6 months when I google this again)

Solution 8 - Git

I've had luck recovering the commit by looking at the reflog, which was located at .git/logs/HEAD

I then had to scoll down to the end of the file, and I found the commit I just lost.

Solution 9 - Git

We'll git log sometimes is not good to get all commits detail, so to view this...

For Mac: Get into you git project and type:

$ nano .git/logs/HEAD

to view you all commits in that, or:

$ gedit .git/logs/HEAD

to view you all commits in that,

then you can edit in any of your favourite browser.

Solution 10 - Git

@bsimmons

git fsck --lost-found | grep commit

Then create a branch for each one:

$ git fsck --lost-found | grep commit
Checking object directories: 100% (256/256), done.
dangling commit 2806a32af04d1bbd7803fb899071fcf247a2b9b0
dangling commit 6d0e49efd0c1a4b5bea1235c6286f0b64c4c8de1
dangling commit 91ca9b2482a96b20dc31d2af4818d69606a229d4

$ git branch  branch_2806a3 2806a3
$ git branch  branch_6d0e49 6d0e49
$ git branch  branch_91ca9b 91ca9b

Now many tools will show you a graphical visualization of those lost commits.

Solution 11 - Git

Actually git fsck can be used to find all the lost commits, you just need the right option:

git fsck --unreachable --no-reflogs

--unreachable alone is not enough because some commits might still be referenced by the reflog. If you need a pretty clear view of the entire commit history, you can create an alias for something like this:

git log --all --decorate --oneline --graph $(git fsck --no-reflogs --unreachable | awk '{if ($2 == "commit") print $3}')

To be honest I am not sure if in this last command you need the --unreachable option, given that git log traverse the ancestors by default (unless --no-walk is specified). I would not bet on it, but I think it is not necessary.

Solution 12 - Git

If you use Git Extensions GUI it can show you a graphical visualization of dangling commits if you check "View -> Show reflog references". This will show dangling commits in the tree, just like all other referenced ones. This way it is way easier to find what you are looking for.

See this image for demonstration. Commits C2, C3, C4, and C5 on the image are dangling but still visible.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmadanView Question on Stackoverflow
Solution 1 - GitkenorbView Answer on Stackoverflow
Solution 2 - GitaraqnidView Answer on Stackoverflow
Solution 3 - GitKieranView Answer on Stackoverflow
Solution 4 - GitSonhjaView Answer on Stackoverflow
Solution 5 - GitFlorian FidaView Answer on Stackoverflow
Solution 6 - GitsultanmyrzaView Answer on Stackoverflow
Solution 7 - GitbsimmonsView Answer on Stackoverflow
Solution 8 - GitGameScriptingView Answer on Stackoverflow
Solution 9 - GitVinod JoshiView Answer on Stackoverflow
Solution 10 - GityakodaView Answer on Stackoverflow
Solution 11 - GitMarco LuzzaraView Answer on Stackoverflow
Solution 12 - GitZdovcView Answer on Stackoverflow