Linux – Automatically chmod a file when doing git add

chmodgitlinuxwindows

I have an ubuntu development system using git to manage files, but I like to do my editing in QTCreator on a different (windows) system, so I have a windows network drive mapped to the ubuntu server.

However, it seems that whenever I save a file, it adds the execute flag on windows. Sometimes it changes from 664 to 764, and sometimes I've seen it change to 777.

So I've been doing a chmod 644 before I do my git add, but it's rather a pain in the ass, and sometimes I forget.

Is there a good way to either prevent windows/QT from adding the +x, or just have "git add" automatically chmod 664 my .c and .h files before staging/committing?

Thanks

Best Answer

Edit:

A solution might be to set core.fileMode variable to false in your ~/.gitconfig. This makes git ignore the executable bit.

If you are using a FAT filesystem, you might want to set core.ignorecase to true as well.

Old answer

Assuming you commit on the Linux box, you can use a hook, in this case .git/hooks/pre-commit:

#!/bin/sh
find . -type f -not -perm 0644 -exec chmod 644 {} \+
find . -type d -not -perm 0755 -exec chmod 755 {} \+
git add .

This is somewhat crude in the sense that forces all files and directories to a certain permission. You might want to add chmod commands for specific files if they need to have other permissions between the find and git add. On the other hand it is short and simple.

A more complex solution might use git diff --cached --name-only to check which files are actually going to be committed and would fix only those. But this would be more complex and wouldn't fix random permission changes.

#!/bin/sh
FILES=$(git diff --cached --name-only)
chmod 644 $FILES
Related Question