Git 如何追蹤檔案可執行權限(+x)

介紹 Git 在 Linux 下如何追蹤檔案可執行權限,並提供新增、取消與驗證可執行位的實用指令。

在 Linux 環境中,Git 會追蹤檔案的可執行位(+x)。 如果你希望腳本在倉庫中被當作「可執行檔」保留下來,需要用 Git 明確記錄這次權限變更。

為檔案新增可執行權限

1
2
3
git update-index --chmod=+x script.sh
git commit -m "chore: mark script.sh as executable"
git push

這條指令會把 script.sh 的可執行位變更加入暫存區。提交並推送後,其他人在拉取或克隆倉庫時也會保留這個權限狀態。

取消檔案可執行權限

1
2
3
git update-index --chmod=-x script.sh
git commit -m "chore: remove executable bit from script.sh"
git push

驗證是否生效

可以用下面的指令檢查工作區權限:

1
2
git clone xxxxxxxxxxxxxxx
ls -l script.sh

如果看到類似 -rwxr-xr-x,表示檔案包含可執行權限;如果是 -rw-r--r--,則表示不可執行。

說明

  • git update-index --chmod=+x/-x 只會修改 Git 記錄的檔案模式,不會替代你對檔案內容本身的修改。
  • 在團隊協作中,建議把這類權限調整獨立提交,便於審查與追溯。
记录并分享
使用 Hugo 建立
主題 StackJimmy 設計