How does git diff handle changes in file permissions?

Git is a powerful and widely used version control system that is primarily used to track and manage changes to code, but it can also monitor changes in file permissions. While many users don’t often interact with this feature, it’s an important part of how Git works.

By default, Git tracks two permissions: executable and non-executable. When you change a file’s permissions and then ask Git to tell you what’s been modified with the ‘git diff’ command, Git will compare the file permission of the working copy to the file permission stored in the index (stage area).

Here’s how it might look:

$ chmod +x your-file
$ git diff

You would see something like this:

diff --git a/your-file b/your-file
old mode 100644
new mode 100755

The numbers you see are a representation of the file’s permissions in octal format. In this case, 100644 represents a non-executable file and 100755 an executable one.

If you want to stage these changes, you can use the ‘git add’ command like so:

$ git add your-file

If you want to commit these changes, you can use the ‘git commit’ command:

$ git commit -m "Change file permissions"

Now, your changes to the file permissions are a part of your repository’s history.

If you’re interested in more than just the executable bit, you might find yourself out of luck with native Git. Git was designed to track content rather than metadata, so it doesn’t handle things like changes in read or write permissions.

However, there are ways to get Git to track these changes by using a ‘pre-commit’ hook that checks for permission changes and includes those in the commit if necessary. But this is more advanced usage and may not be necessary for most users.

In summary, Git diff can show changes in executable permissions between the working copy and the index. Other permission changes are not tracked by default, but there can be ways to incorporate them if necessary.