Home > Software design >  git diff displaying wrong file mode - instead of 775 displaying 755
git diff displaying wrong file mode - instead of 775 displaying 755

Time:07-06

I have recenlty changed system and few files started appearing in git diff. Its due to mode change and am ok with that for now.

I am wondering why git diff displaying mode 755, when it supposed to 775. In below screen all linux command says it is 775, but git diff says it is 755.

OS is Ubuntu 22.04.

enter image description here

CodePudding user response:

Git only stores one bit (as in binary digit) of "mode" information per ordinary file: "executable" ( x) or "not executable" (-x). This single bit of mode information is, however, stored as mode 100755 ( x) or mode 100644 (-x).

It's no coincidence that 100755 corresponds to a Linux 0755 file mode: in fact, the 100 part is from S_IFREG in <sys/stat.h>. Likewise 100644 corresponds to a file whose mode is 0644 or rw-r--r--. In the distant past, Git did store more mode bits per file. But this was discovered to be a mistake, so now Git stores only the one mode bit—but uses the same encoding it used back when it stored more bits.

The actual file permission bits that you'll find on disk will depend on your umask setting, not on the mode 100755 setting. If you have umask 022, Git will create executable files with mode 0755 or rwxr-xr-x. If you change your umask to 002, Git will create such files with mode 0775 or rwxrwxr-x. But the old and/or new mode as shown in git diff output will always be either 100644 or 100755, and that means -x or x respectively.

CodePudding user response:

When storing a file (blob) in Git, there are only two possible file modes: 644 or 755. That is, Git stores only the executable bit, and if it is set, it stores the latter, and if not, it stores the former. Thus, for diffs, Git will only reflect whether the executable bit is set and will always use one of those two modes.

In the working tree, Git uses the umask to set permissions, which explains why your files are actually 775.

  • Related