DEV Community

Ryo Kuroyanagi
Ryo Kuroyanagi

Posted on

Chmod +x by Git on Windows

TL;DR The following Git command is chmod +x equivalent. You can run it on your Windows machine and the permission is reflected to git repo once you commit the change.

git update-index --chmod=+x script.sh
Enter fullscreen mode Exit fullscreen mode

Background

I've been a mac user for long time since I started coding. However, I'm Windows system recently for machine learning / 3DGC. Windows OS has the different file permission management system and it's not compatible to Linux / macOS.

I use Github Actions to run tests and build on Ubuntu. Sometimes bash script is used to pack complicated commands into scripts used in Actions. I found that the bash scripts can be run on Windows with Git for Windows terminal even the Linux execution permission is not set. However, once I push my scripts and try to run it on Ubuntu on Github Actions, it does not work because of a lack of permission.

Change permission and check if it has changed

As I put at the top of this article, the following command gives the execution permission to a script.

git update-index --chmod=+x script.sh
Enter fullscreen mode Exit fullscreen mode

git ls-tree head command shows the permissions like the below when a script file script.sh is created on Windows. It says the permission is 644.

100644 blob <hash> script.sh
Enter fullscreen mode Exit fullscreen mode

After we have used the chmod equivalent command, Git automatically stages the change. Once, we commit the change, the permission changes to like the below. Now we see 755 permission of the file.

100755 blob <hash> script.sh
Enter fullscreen mode Exit fullscreen mode

That's it ! Hope this helps who are working on cross platforms.

Top comments (1)

Collapse
 
alastairs profile image
Alastair Smith • Edited

For anyone else looking for an alias, I created it like so:

git config --global alias.chmod "update-index --chmod"
Enter fullscreen mode Exit fullscreen mode

which can then be invoked like git chmod +x script.sh.