Updating file permissions with git-bash on Windows 7
Asked Answered
N

3

73

How do I update file permissions with git-bash on Windows 7?

I've tried the following without success:

$ ls -al scripts/script.sh
-rw-r--r--    1 myUid   Administ       70 Sep  8 11:24 scripts/script.sh

$ git update-index --chmod=+x scripts/script.sh

$ ls -al scripts/script.sh
-rw-r--r--    1 myUid   Administ       70 Sep  8 11:24 scripts/script.sh

$ chmod +x scripts/script.sh

$ ls -al scripts/script.sh
-rw-r--r--    1 myUid   Administ       70 Sep  8 11:24 scripts/script.sh
Nuthouse answered 8/9, 2014 at 17:42 Comment(0)
S
103

You are probably using NTFS or FAT32 on Windows, and those filesystems do not support the executable permission. Instead, cygwin looks at the file name and contents to determine whether it's executable:

Files are considered to be executable if the filename ends with .bat, .com or .exe, or if its content starts with #!.

So you should make sure that the bash file starts with a shebang (e.g. #!/bin/bash). Then, you should be able to just execute the file, disregarding the permission output of ls.

Slap answered 8/9, 2014 at 17:45 Comment(3)
What is a shebang ?Homely
@EdwardBlack something like #/bin/bash, I clarified the question accordingly.Slap
What about .CMD? :-)Ferryboat
S
68

If you're updating scripts in a windows environment that are being deployed to a linux filesystem, even though they are permitted to run locally, you may still find yourself needing to grant execute before pushing.

From this article on Change file permissions when working with git repo's on windows:

  1. Open up a bash terminal like git-bash on Windows
  2. Navigate to the .sh file where you want to grant execute permissions
  3. Check the existing permissions with the following command:

    git ls-files --stage 
    

    Which should return something like 100644

  4. Update the permissions with the following command

    git update-index --chmod=+x 'name-of-shell-script'
    
  5. Check the file permission again

    git ls-files --stage 
    

    Which should return something like 100755

  6. Commit changes and push!

git bash on windows

Shechem answered 21/8, 2019 at 4:52 Comment(3)
Helpful answer. Can yo please tell me what is this software you have used to tab git bash windows?Prog
@LahiruChandima, Windows Terminal - highly recommend.Shechem
This is exactly the answer I was looking for. In my case the git ls-files --stage reveals that they are all 644 already and that MSYS is marking some of them executable by heuristic.Haberdasher
C
11

Very likely you do not have "shebang" at the beginning of your script. So Windows does not know which binary should be used to run the script. Thus the permissions gets silently ignored.

For example:

#!/usr/bin/sh

or

#!/usr/bin/bash

It looks like git-bash detects it automaticly, because the executable attribute gets set even without chmod command. Moreover it is not possible to disable it by using chmod.

Clove answered 9/4, 2021 at 1:14 Comment(1)
Yes, it seems if your file doesn't start with #!, like #!/usr/bin/bash then the chmod command doesn't turn on the x flag. It is quite weird to me, the content of the file and the permission are independent in linux/unix.Hanway

© 2022 - 2024 — McMap. All rights reserved.