English

Gitで、Windowsでも実行権限を付けてコミットする方法

​​Git​Linux,Windows,コマンドライン,シェルスクリプト

git update-index --chmod=+x ファイル名 を実行すると、ファイルそのものの権限を変えなくても、Git のインデックスに「実行権限あり」と記録し、そのままコミットできます。Windows で作ったシェルスクリプトを Linux のサーバーや CI でクローンして実行したら Permission denied になった、というときに使います。操作の前と後で、Git の記録と、Linux 側でクローンしたときの動きがどう変わるかを、実際の出力で示します。

補足

検証環境は Ubuntu 24.04、Git 2.43.0 です。Windows の実機はこの環境にないため、Ubuntu 上の手元のクローンで core.fileMode を false にして、実行権限を扱えない環境の Git の代わりにしました。Windows 上の Git での動きは試していません。作者とコミッターは例示用の値(Neko <neko@example.com>)です。共有のリポジトリは同じフォルダーに置いた bare リポジトリで、リモートの URL は相対パスの ../origin.git にしています。

Windows で chmod が効かない理由と core.fileMode

git config の公式リファレンスの core.fileMode の項によると、この設定は、作業フォルダーのファイルの実行権限を Git が見るかどうかを決めます。git init や git clone は、ファイルシステムが実行権限を正しく扱えるかを調べて、この値を自動で設定します。同じ項には、実行権限が失われる例として、Cygwin で作ったリポジトリを Git for Windows で開く場合が挙がっています。

core.fileMode が false の Git は、ファイルの実行権限を見ません。手元のリポジトリの値は、次のコマンドで確かめられます。検証用のクローンでは false にしてあります。

console
$ git config core.fileMode
false

git update-index の公式リファレンスの CONFIGURATION の節にも、実行権限があてにならないファイルシステムでは core.filemode を false にし、その場合は git update-index --chmod= を使う必要が出ることがある、と書かれています。

操作の前:100644 で記録され、Linux では実行できない

hello.sh を、ふつうに git add してコミットし、共有のリポジトリに push しました。git ls-files -s の先頭の数字が、インデックスに記録されたファイルのモードです。100644 は実行権限のないふつうのファイル、100755 は実行権限のあるふつうのファイルを表します(インデックスの形式の公式リファレンスでは、ふつうのファイルの権限は 0755 と 0644 だけが使えるとされています)。

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

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

 create mode 100644 hello.sh

この状態で、共有のリポジトリと同じ場所に、core.fileMode が true のままの Linux 側のクローンを作って実行すると、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 では、Git は変更に気づかない

core.fileMode が false の手元のクローンで、ファイルに chmod +x を実行してみます。ファイルの権限は -rwxr-xr-x になりましたが、git status --short は何も表示せず、git diff も何も出しません。インデックスの記録は 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

core.fileMode が false だと、ファイルの権限を変えても、Git は変更として扱わず、インデックスの記録も変わりません。確かめたあと、chmod -x で権限を元に戻しました。

git update-index –chmod=+x で付ける

ファイル名を指定して実行します。何も表示されずに終わりました。

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

操作の後は、インデックスの記録が 100755 に変わりました。git status では、ステージ済みの変更(Changes to be committed)として出ます。git diff --cached では、中身の差分はなく、モードの変更だけが出ました。

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

変わったのはインデックスの記録だけです。手元のファイルの権限は -rw-r--r-- のままでした。

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

コミットして push する

ふつうに git commit します。出力に mode change 100644 => 100755 が出ました。git log --summary でも、どのコミットで実行権限を付けたかが分かります。

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

共有のリポジトリに push したあとの git status --short は、何も表示しませんでした。

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

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

手元のファイルは -rw-r--r-- のままですが、core.fileMode が false なので、インデックスとの違いとしては扱われません。

Linux 側でクローンすると実行できる

共有のリポジトリと同じ場所に、core.fileMode が true の Linux 側のクローンを改めて作りました。ファイルは -rwxr-xr-x で作られ、./hello.sh がそのまま動きました。

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

中身の変更も一緒にステージされる

git update-index --chmod は、権限だけでなく、そのときのファイルの中身もインデックスに入れます。hello.sh に1行足した状態で --chmod=-x を実行すると、モードの変更と一緒に、足した行もステージされました。git status --short の表示も、ステージしていない変更( M)から、ステージ済みの変更(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"

git update-index の公式リファレンスでも、--chmod は「更新するファイル(the updated files)の実行権限を設定する」と説明されています。権限だけをコミットしたいときは、中身の変更を先に片付けてから実行します。この検証では、git reset でステージを取り消し、git restore で中身も元に戻してから、次の節で権限を外しました。

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

–chmod=-x で実行権限を外す

付けるときの +x を -x にすると、外せます。git diff --cached --summary で、ステージした変更がモードだけであることを表示してからコミットしました。

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

新しいファイルは git add –chmod=+x で入れる

まだ Git に入れていないファイルには、git update-index --chmod=+x は使えません。次のエラーで止まり、終了コードは 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

新しいファイルは、git add に --chmod=+x を付けて追加します。git add の公式リファレンスにあるとおり、変わるのはインデックスの記録だけで、手元のファイルは変わりません。最初から 100755 で入り、コミットにも 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

core.fileMode が true の環境では、ファイルにも chmod +x をする

Linux 側のクローンで、test.sh を実行権限なしのまま追加してコミットしておきます。

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

この状態で、chmod をせずに git update-index --chmod=+x だけを実行すると、git status --short に MM と出ました。インデックスは 100755 になった一方で、手元のファイルは実行権限がないままなので、git diff には「100755 から 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

ファイルにも chmod +x をすると、インデックスと手元のファイルがそろい、ステージしていない変更は消えて M だけになりました。

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

新しいファイルなら、先に chmod +x を実行してから git add するだけで、--chmod を付けなくても 100755 で入りました。

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