36

I wanted to make some files in git read only. But I couldn't find any good documentation on doing this.

Does git store the read, write, execute permissions for files?

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177

2 Answers2

39

According to kernel.org git does not store all the permissions possible for files.

Git is a content tracker, where content is de facto defined as "whatever is relevant to the state of a typical sourcecode tree". Basically, this is just files' data and "executable" attribute.

So git stores only the content in the file and the execute bit.

(This is probably a design choice. Probably driven partly by the fact that not all file systems are the same.)

Trevor Boyd Smith
  • 18,164
  • 32
  • 127
  • 177
  • 8
    That's correct. The extra x bit is set in the committed version if any of the execute bits are set on the original, and otherwise not. Nonetheless, the mode of each file is `0644` or `0755`, which *seems* to have provisions for additional bits. A comment in `fsck.c` mentions that some very old repos have files with mode `0664`, i.e., Git did at one point keep group-write permissions. This turned out to be a mistake and was deliberately changed. – torek Sep 23 '16 at 19:54
3

git update-index --chmod=+x foo.sh

TheEagle
  • 5,808
  • 3
  • 11
  • 39
  • 1
    Related questions: https://stackoverflow.com/questions/40978921/how-to-add-chmod-permissions-to-file-in-git https://stackoverflow.com/questions/3207728/retaining-file-permissions-with-git – Anton Tarasenko Jun 05 '20 at 01:10
  • 13
    What does this command do? How does it answer the question? – Scz Jul 13 '20 at 00:18
  • 1
    @Scz haha yeah it's funny, but his answer is the reason why I searched for this post, and the other comment points to a more detailed answer. – elmiomar Mar 05 '21 at 11:23