如何在Windows上使用Git创建一个可执行脚本?

长话短说,今天介绍如何在windows上 使用Git上创建一个可执行的shell脚本

首先我们要知道windows上Git默认添加的文件权限是:-rw-r–r–(对应权限值是644),而通常创建的shell脚本都希望天然可执行,故有必要在Windows上使用Git管理shell脚本时保证可执行权限。

正统姿势(一次Git 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

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

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

文件权限位由 -rw-r–r– (644)  变为了  -rwxr-xr-x (755 可执 ) 。 

我们是在索引区覆盖文件的可执行位。

btw, 从Git 2 .9开始,您可以在一个命令中暂存文件并设置可执行标志:

git add --chmod=+x path/to/file