Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List ALL the files that ever existed in ANY branch in a git repository

Tags:

git

I have some files I'm looking for, but that I don't know where they are located in my git repository (both in space and time). I also speculate it is possible that both the files and the branch in which they reside might be deleted.

How could I list every file ever in existence in the whole git repository? I know deleted files / branches could pretty well have already been garbage collected, and well, there's nothing to do about it than to pray it didn't happen already.

Thanks!

like image 267
devoured elysium Avatar asked Jan 21 '13 16:01

devoured elysium


People also ask

How do I get a list of files in git?

This command will list the files that are being tracked currently. If you want a list of files that ever existed use: git log --pretty=format: --name-only --diff-filter=A | sort - | sed '/^$/d'This command will list all the files including deleted files.

How do I see files in a git repository?

The Git Show command allows us to view files as they existed in a previous state. The version can be a commit ID, tag, or even a branch name. The file must be the path to a file. For example, the following would output a contents of a file named internal/example/module.go file from a tagged commit called “release-23”.

Where does git store file history?

Git stores the complete history of your files for a project in a special directory (a.k.a. a folder) called a repository, or repo. This repo is usually in a hidden folder called . git sitting next to your files.

Which of the following contains all the files for project in git?

A repository contains all of your project's files and each file's revision history.


1 Answers

This is a bit crazy, but it might do what you want:

#!/bin/sh

content="$@"

for hash in $({
    git rev-list --objects --all
    git rev-list --objects -g --no-walk --all
    git rev-list --objects --no-walk \
        $(git fsck --unreachable |
          grep '^unreachable commit' |
          cut -d' ' -f3)
    git fsck --unreachable | grep "^unreachable blob" | cut -d' ' -f3
} 2> /dev/null | cut -d' ' -f1 | sort | uniq); do
    if git cat-file blob $hash 2> /dev/null | grep -i $content > /dev/null ; then
        echo $hash
    fi
done

I tried to get absolutely every blob accessible, and look for a specific content in each of them.

You'll be able to do a git cat-file -p hash on each of the generated hashes and might find your lost file.


On a side note, I'm surprised to not see any nice way to list absolutely every object available in the repository. I was thinking of unpacking everything and go from there, that could be a better solution too.


Resources:

  • git documentation - cat-file
  • git documentation - rev-list
  • git documentation - fsck

Related topics:

  • Git - how to list ALL objects in the database
like image 175
Colin Hebert Avatar answered Sep 22 '22 11:09

Colin Hebert