391

I'm using SparkleShare, which uses Git to sync files between my laptop and my backup server.

Now I want to be able to browse the files and dirs that I've uploaded to my server, but I do not know how.

I understand that Git uses some sort of special file hierarchy and that I cannot just list my files, right?

But what would I have to do to list them and browse my files?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Daniel Holm
  • 4,347
  • 3
  • 19
  • 27
  • Flagged as duplicate of https://stackoverflow.com/questions/15606955/how-can-i-make-git-show-a-list-of-the-files-that-are-being-tracked/69869399 even though this question is older, as the other one is more clearly phrased and has more answers so SO stays tidy – xeruf Nov 07 '21 at 02:44

3 Answers3

636

This command:

git ls-tree --full-tree -r --name-only HEAD

lists all of the already committed files being tracked by your git repo.

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
karlphillip
  • 92,053
  • 36
  • 243
  • 426
390

Try this command:

git ls-files 

This lists all of the files in the repository, including those that are only staged but not yet committed.

http://www.kernel.org/pub/software/scm/git/docs/git-ls-files.html

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
amadillu
  • 4,349
  • 1
  • 16
  • 15
  • 4
    Same output as the answer above, but with --name-only (Short format). Nice porcelain. – Ron E Jun 07 '14 at 19:41
  • 26
    One thing to note: the accepted answer works on a bare repo, but this answer does not. – Trebor Rude Aug 22 '14 at 15:59
  • 1
    This Git command makes a much cleaner output and is ideal if you just want the list of files tracked by Git. – mico Feb 27 '16 at 12:00
  • 1
    is there a way to list only direcotries.. something like `git ls-directories` ? – Ramesh Pareek May 25 '16 at 11:08
  • 17
    **Warning:** if you are in a subdirectory, it only lists files under that tree - the question states "all of the files in the repository" but that's only true if you're in the root of the repo. – wim Mar 16 '17 at 18:55
  • With git 2.6.12 I need `-r HEAD` to get any output from `git ls-files` without an error, even the subtree. I agree the accepted answer is better to recurse from the top. – Grant Bowman Mar 26 '18 at 18:59
  • 1
    I've used `git ls-files --full-name :/`. `:/` means the root directory of the repository, while `--full-name` outputs paths relative to the root directory of the repository rather than the working directory. – Matthew White Jun 03 '18 at 21:36
  • And to list just the tracked files from a certain directory: `git ls-files some/dir`. – Gabriel Staples Aug 29 '20 at 16:57
  • @TreborRude works fine on a bare repo for me: Inside the bare repo: `git ls-files` Outside I just need to specify location of the repo (the same way I need to for any git command) `git --git-dir=$HOME/.dotfiles.git/ ls-files`. Maybe things have changed since you wrote that? – Gerry Jul 27 '21 at 14:46
5

git ls-tree --full-tree -r HEAD and git ls-files return all files at once. For a large project with hundreds or thousands of files, and if you are interested in a particular file/directory, you may find more convenient to explore specific directories. You can do it by obtaining the ID/SHA-1 of the directory that you want to explore and then use git cat-file -p [ID/SHA-1 of directory]. For example:

git cat-file -p 14032aabd85b43a058cfc7025dd4fa9dd325ea97
100644 blob b93a4953fff68df523aa7656497ee339d6026d64    glyphicons-halflings-regular.eot
100644 blob 94fb5490a2ed10b2c69a4a567a4fd2e4f706d841    glyphicons-halflings-regular.svg
100644 blob 1413fc609ab6f21774de0cb7e01360095584f65b    glyphicons-halflings-regular.ttf
100644 blob 9e612858f802245ddcbf59788a0db942224bab35    glyphicons-halflings-regular.woff
100644 blob 64539b54c3751a6d9adb44c8e3a45ba5a73b77f0    glyphicons-halflings-regular.woff2

In the example above, 14032aabd85b43a058cfc7025dd4fa9dd325ea97 is the ID/SHA-1 of the directory that I wanted to explore. In this case, the result was that four files within that directory were being tracked by my Git repo. If the directory had additional files, it would mean those extra files were not being tracked. You can add files using git add <file>... of course.

Jaime Montoya
  • 6,915
  • 14
  • 67
  • 103
  • 1
    You could also just `cd` to the directory that you're interested in and omit `--full-tree`. – Nathan Feb 23 '20 at 04:37
  • @Nathan will that still work with bare repos or whenever the worktree is entirely elsewhere? The question was about repo not worktree after all. – 0xC0000022L Feb 22 '23 at 15:23