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?

1

3 Answers 3

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.

1
  • This works only when we have at least one commit.
    – zkutch
    Mar 18, 2022 at 2:37
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

8
  • 4
    Same output as the answer above, but with --name-only (Short format). Nice porcelain.
    – Ron E
    Jun 7, 2014 at 19:41
  • 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, 2016 at 12:00
  • 1
    is there a way to list only direcotries.. something like git ls-directories ? May 25, 2016 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, 2017 at 18:55
  • 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. Jun 3, 2018 at 21:36
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.

2
  • 1
    You could also just cd to the directory that you're interested in and omit --full-tree.
    – Nathan
    Feb 23, 2020 at 4: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. Feb 22 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.