2

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

2 Answers 2

4

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
5
  • Hmm, very dangerous haha. Had to git reset, it modified and added a whole bunch of other files ;) Is there any way to make it so that it only modifies the files which are already staged for commit? Edit: Just saw the 2nd paragraph, yeah, that would be better. How would one go about that? I don't care about random permission changes except for the files that are being submitted
    – Jordan
    Nov 25, 2013 at 23:50
  • @Jordan: The second para should use git diff. See updated answer. Nov 26, 2013 at 0:18
  • Hmm, that seems to have chmodded the files after the commit, or at least not add the files with the modified permissions to the staged files. I tried doing "git add $FILES" but it didn't seem to like that
    – Jordan
    Nov 26, 2013 at 22:08
  • @Jordan There might be a better way. See updated answer. Dec 9, 2013 at 22:35
  • Aha! That's exactly what I'm looking for. Thanks!
    – Jordan
    Dec 11, 2013 at 19:48
0

In case you need to fix files in an existing repo, for example executable bit, try this:

git ls-files “*.sh” | xargs git update-index –add –chmod=+x
git ls-files “*.exe” | xargs git update-index –add –chmod=+x

git ls-files is faster than "find"

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .