gitgit-ls-files

What is the meaning of the different fields in the output of `git ls-files -s`?


In Git, a typical line of the result returned by command git ls-files -s looks like

100755 be2c2e9b0966253096472d4b482c458bc892e493 0 .gitignore

What do those fields mean?


Solution

  • Look no further than the git ls-files man page:

    git ls-files just outputs the filenames unless --stage is specified in which case it outputs:

           [<tag> ]<mode> <object> <stage> <file>
    

    (The --stage flag is equivalent to -s.)

    What do those fields mean?


    You also ask, in one of your follow-up comment,

    What's the relation between the <object> and the <file>?

    They're completely independent, since only the contents of a file (not its path/filename) are used to generated the hash associated with it. To convince yourself of that, you can conduct the following experiment in a toy repository:

    # Set things up
    $ mkdir testgit
    $ cd testgit/
    $ git init
    
    # Write the same contents to two files
    $ printf "foo\n" > README.md
    $ printf "foo\n" > bar.txt
    
    # Stage the two files and run git ls-files
    $ git add .
    $ git ls-files -s
    
    100644 257cc5642cb1a054f08cc83f2d943e56fd3ebe99 0    README.md
    100644 257cc5642cb1a054f08cc83f2d943e56fd3ebe99 0    bar.txt
    

    Note that, even though the two files have different names, they have identical SHAs, since they have the same contents.