41

I want to access a bare git repository, and I want to list all files in the repository.

On a normal git repository I can easily do that by running git ls-files.

Example output:

$ git ls-files
README.md
file1.js
file2.js
file3.js
folder1/file4.js
folder2/file5.js

In a bare git repository this fails silently. It just doesn't return any files (but exits successfully):

$ cd my-bare-repository
$ git ls-files #returns nothing
$ echo $? #print exit code from previous command
$ 0

Now I am aware that I have to provide a meaningful branch or master to display. But how can I actually get this list of the files that I know are in my repository?

Jesper Rønn-Jensen
  • 91,561
  • 40
  • 112
  • 147

1 Answers1

66

You can try the other command which list files:

git ls-tree --full-tree -r HEAD

According to this comment, the command git ls-tree works in bare repo.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Thanks, this works. However, in my current example, I get the same output with and without `--full-tree` – Jesper Rønn-Jensen Sep 18 '14 at 07:09
  • 6
    @JesperRønn-Jensen you also have `git ls-tree --full-tree -r --name-only HEAD` , to list just the file names, (without leading cruft about permissions and blob SHA values) – VonC Sep 18 '14 at 07:11
  • I am accepting this answer since it's more than I asked for:) +10 for correctness, +10 for fast answer :) – Jesper Rønn-Jensen Sep 18 '14 at 07:15
  • `git ls-files` works for me in a bare repository with git 2.27, and probably has supported it for a while now. – Victor Roetman May 28 '21 at 12:51
  • @VictorRoetman Yes: it could list files in the index. by default (https://git-scm.com/docs/git-ls-files) And technically, a bare repository can have an index (https://github.com/libgit2/libgit2sharp/issues/1491#issuecomment-328624372). But in my bare repos, `git ls-files` returns nothing. – VonC May 28 '21 at 19:36