How to create file execute mode permissions in Git on Windows?

I use Git in Windows, and want to push the executable shell script into git repo by one commit.

Usually I need to do two steps (git commit).

$ vi install.sh
$ git add install.sh  
$ git commit -am "add new file for installation" # first commit
[master f2e92da] add support for install.sh
 1 files changed, 18 insertions(+), 3 deletions(-)
 create mode 100644 install.sh
$ git update-index --chmod=+x install.sh
$ git commit -am "update file permission"        # second commit
[master 317ba0c] update file permission
  0 files changed
  mode change 100644 => 100755 install.sh

How can I combine these two steps into one step? git configuration? windows command?

Remind: Two answers are good, git add --chmod=+x file is supported in new git version

Reference: see question in Git file permissions on Windows for second commit

5 s
5

There’s no need to do this in two commits, you can add the file and mark it executable in a single commit:

C:\Temp\TestRepo>touch foo.sh

C:\Temp\TestRepo>git add foo.sh

C:\Temp\TestRepo>git ls-files --stage
100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0       foo.sh

As you note, after adding, the mode is 0644 (ie, not executable). However, we can mark it as executable before committing:

C:\Temp\TestRepo>git update-index --chmod=+x foo.sh

C:\Temp\TestRepo>git ls-files --stage
100755 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0       foo.sh

And now the file is mode 0755 (executable).

C:\Temp\TestRepo>git commit -m"Executable!"
[master (root-commit) 1f7a57a] Executable!
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 foo.sh

And now we have a single commit with a single executable file.

Leave a Comment