«

Git: Marking a file as executable

Recently I've moved to windows as my main development platform and have been learning all the joy that comes with it. I'll be doing a blog post series soon on how to move from a *nix environment to windows 10 soon but in the meantime I thought I'd share this handy git tip before I forget about it.

The scenario

I clone my repositories using either git bash or inside the Windows Subsystem Linux onto one of the mounted drives. If you use WSL this would be in a location like /mnt/c/Users/<username> for example. When you look at files in those folders they are always owned by root and by default have 777 permissions.

Now git inside WSL is smart enough to know that the mount is NTFS or FAT so it doesn't commit the file permissions and just uses 644 by default. Yay for security!

The problem is, if you create a bash script for automagic use in your Continuous Integration pipeline or some sort of automated system, your script isn't going to be executable for the system that wants to run it.

The solution

The simple solution is that when you stage a file or change on the script for commit you can just run git update-index --chmod=+x <files...> to mark your files as executable. What a win!

The next step is to commit the change which you'll see staged in the index if you run git status. You're done!

Make it an alias

Now to make things simpler for you in the future, just run git config --global alias.me 'update-index --chmod=+x' and you can now make a file executable just by running git me <files...>. Now commit and you're done!

If this helped you or saved you some time, contact me on twitter and let me know. If it made you sigh in relief than please also consider sharing this on your social networks so that others can benefit from this information too!

Share Comment on Twitter