日本語

How to Commit Execute Permission on Windows with Git

​​Git​command line,Linux,shell script,Windows

Running git update-index --chmod=+x filename records the file as executable in the Git index and lets you commit it, without changing the file's permissions on disk. Use this when a shell script you created on Windows fails with Permission denied after you clone and run it on a Linux server or in CI. This article shows, with actual output, how Git's record and the behavior of a Linux clone change before and after the operation.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. No real Windows machine was available, so I set core.fileMode to false on a local clone on Ubuntu to stand in for a Git environment that cannot handle execute permissions. I did not test the behavior of Git on Windows. The author and committer are placeholder values (Neko <neko@example.com>). The shared repository is a bare repository in a sibling folder, and the remote URL is the relative path ../origin.git.

Why chmod has no effect on Windows and core.fileMode

According to the core.fileMode entry in the official git config reference, this setting determines whether Git checks the execute permission of files in the working tree. git init and git clone test whether the file system handles execute permissions correctly and set this value automatically. The same entry gives an example of lost execute permissions: a repository created with Cygwin and opened with Git for Windows.

A Git with core.fileMode set to false does not look at file execute permissions. You can check the value in your repository with the following command. In the test clone, it is set to false.

console
$ git config core.fileMode
false

The CONFIGURATION section of the official git update-index reference also says that on a file system where execute permissions are unreliable, you should set core.filemode to false, and in that case you may need to use git update-index --chmod=.

Before the operation: recorded as 100644, not executable on Linux

I added hello.sh with a normal git add, committed it, and pushed it to the shared repository. The leading number in the output of git ls-files -s is the mode recorded in the index for the file. 100644 is a regular file without execute permission, and 100755 is a regular file with execute permission. (The official index format reference states that only 0755 and 0644 are valid permissions for regular files.)

console
$ git ls-files -s
100644 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh

$ git log --summary --format="%h %s"
cf71537 hello.sh を足す

 create mode 100644 hello.sh

In this state, I created a Linux clone in the same location as the shared repository, with core.fileMode left at true, and ran the script. It stopped with Permission denied.

console
$ git clone -q origin.git before 2>/dev/null; cd before && git config core.fileMode && stat -c "%A %n" hello.sh && ./hello.sh; echo "exit=$?"
true
-rw-r--r-- hello.sh
bash: line 1: ./hello.sh: Permission denied
exit=126

chmod +x alone: Git does not notice the change

In the local clone with core.fileMode set to false, I ran chmod +x on the file. The file permissions became -rwxr-xr-x, but git status --short printed nothing, and git diff printed nothing either. The index record stayed at 100644.

console
$ chmod +x hello.sh && stat -c "%A %n" hello.sh && git status --short; echo "exit=$?"
-rwxr-xr-x hello.sh
exit=0

$ git diff; git ls-files -s
100644 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh

When core.fileMode is false, Git does not treat a change in file permissions as a change, and the index record does not change either. After confirming this, I restored the permission with chmod -x.

Setting the permission with git update-index –chmod=+x

I ran the command with the file name. It finished with no output.

console
$ git update-index --chmod=+x hello.sh; echo "exit=$?"
exit=0

After the operation, the index record changed to 100755. git status shows it as a staged change (Changes to be committed). git diff --cached showed no content difference, only the mode change.

console
$ git ls-files -s
100755 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   hello.sh

$ git diff --cached
diff --git a/hello.sh b/hello.sh
old mode 100644
new mode 100755

Only the index record changed. The file on disk still had -rw-r--r--.

console
$ stat -c "%A %n" hello.sh
-rw-r--r-- hello.sh

Commit and push

I ran a normal git commit. The output included mode change 100644 => 100755. git log --summary also shows which commit set the execute permission.

console
$ git commit -m "hello.sh に実行権限を付ける"
[main 9fd8749] hello.sh に実行権限を付ける
 1 file changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 hello.sh

$ git log --summary --format="%h %s"
9fd8749 hello.sh に実行権限を付ける

 mode change 100644 => 100755 hello.sh
cf71537 hello.sh を足す

 create mode 100644 hello.sh

After pushing to the shared repository, git status --short printed nothing.

console
$ git push -q origin main; echo "exit=$?"
exit=0

$ git status --short; echo "exit=$?"
exit=0

The file on disk still had -rw-r--r--, but because core.fileMode is false, Git does not treat it as a difference from the index.

Linux clone runs the script

I created a new clone on Linux, in the same location as the shared repository, with core.fileMode set to true. The file was created as -rwxr-xr-x, and ./hello.sh ran as is.

console
$ git clone -q origin.git linux 2>/dev/null; cd linux && git config core.fileMode && git ls-files -s && stat -c "%A %n" hello.sh && ./hello.sh
true
100755 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh
-rwxr-xr-x hello.sh
Nyan

Content changes are staged together with the mode

git update-index --chmod puts the current file content into the index as well as the permission. I added one line to hello.sh and then ran --chmod=-x. The added line was staged along with the mode change. The git status --short output also changed from an unstaged change ( M) to a staged change (M ).

console
$ printf "echo \"nyan nyan\"\n" >> hello.sh && git status --short
 M hello.sh

$ git update-index --chmod=-x hello.sh && git status --short && git diff --cached
M  hello.sh
diff --git a/hello.sh b/hello.sh
old mode 100755
new mode 100644
index 3a9bd35..b6374d5
--- a/hello.sh
+++ b/hello.sh
@@ -1,2 +1,3 @@
 #!/bin/sh
 echo "Nyan"
+echo "nyan nyan"

The official git update-index reference describes --chmod as setting the execute permission of "the updated files." If you want to commit only the permission, first deal with any content changes. In this test, I unstaged the change with git reset, restored the content with git restore, and then removed the permission in the next section.

console
$ git reset -q && git ls-files -s && git status --short
100755 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh
 M hello.sh

$ git restore hello.sh && git status --short; echo "exit=$?"
exit=0

Removing the execute permission with –chmod=-x

Changing +x to -x removes the permission. I used git diff --cached --summary to confirm that the staged change was only the mode, and then committed.

console
$ git update-index --chmod=-x hello.sh && git diff --cached --summary && git commit -q -m "hello.sh の実行権限を外す" && git log --summary --format="%h %s" -1
 mode change 100755 => 100644 hello.sh
f35f090 hello.sh の実行権限を外す

 mode change 100755 => 100644 hello.sh

$ git ls-files -s
100644 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh

For new files, use git add –chmod=+x

You cannot use git update-index --chmod=+x on a file that is not yet tracked by Git. It stopped with the following error, and the exit code was 128.

console
$ printf "#!/bin/sh\necho \"build\"\n" > build.sh && git update-index --chmod=+x build.sh; echo "exit=$?"
error: build.sh: cannot add to the index - missing --add option?
fatal: Unable to process path build.sh
exit=128

For a new file, add it with git add --chmod=+x. As the official git add reference describes, only the index record changes, and the file on disk does not. The file was added as 100755 from the start, and the commit also showed create mode 100755.

console
$ git add --chmod=+x build.sh && git ls-files -s && git status --short
100755 f26c63f344bb36f9c3d776b69a708f7d09845cef 0	build.sh
100644 3a9bd3570ec847245d97f7462364a5bff753e4cc 0	hello.sh
A  build.sh

$ git commit -q -m "build.sh を足す" && git log --summary --format="%h %s" -1
7fd9904 build.sh を足す

 create mode 100755 build.sh

When core.fileMode is true, also run chmod +x on the file

In the Linux clone, I added test.sh without execute permission and committed it.

console
$ git pull -q; printf "#!/bin/sh\necho \"test\"\n" > test.sh && git add test.sh && git commit -q -m "test.sh を足す" && stat -c "%A %n" test.sh
-rw-r--r-- test.sh

In this state, running only git update-index --chmod=+x without chmod showed MM in git status --short. The index became 100755, but the file on disk still had no execute permission. So git diff showed an unstaged change that changes the mode from 100755 back to 100644.

console
$ git update-index --chmod=+x test.sh && git status --short && git diff
MM test.sh
diff --git a/test.sh b/test.sh
old mode 100755
new mode 100644

After also running chmod +x on the file, the index and the file on disk matched, the unstaged change disappeared, and only M remained.

console
$ chmod +x test.sh && git status --short
M  test.sh

For a new file, you only need to run chmod +x before git add, and it is added as 100755 without --chmod.

console
$ printf "#!/bin/sh\necho \"run\"\n" > run.sh && chmod +x run.sh && git add run.sh && git ls-files -s run.sh && git status --short
100755 743ef7307579910a296288bd9924eab11a54a05a 0	run.sh
A  run.sh
M  test.sh

​Posted by 86