如何判断Git仓库中指定版本的文件是否具备可执行权限?
Great question! Git absolutely tracks the executable permission status of files, and there are a couple of straightforward ways to verify this for any commit (including the current HEAD):
1. Use git ls-tree to directly inspect the file mode
This is the most direct method to check the permission mode of a file at a specific commit. Git stores file permissions in a "mode" field, where:
100755means the file is executable (read/write/execute for owner, read/execute for group and others)100644means it's a regular non-executable file (read/write for owner, read-only for group and others)
Run this command, replacing <commit-hash> with your target commit (or HEAD for the current version) and <file-path> with the path to your file:
git ls-tree <commit-hash> -- <file-path>
Example Output:
100755 blob a1b2c3d scripts/deploy.sh
The first field (100755) tells you the file has executable permissions in that commit. If it showed 100644, the file wouldn't be executable.
2. Use git cat-file to view blob details
If you want more granular details about the file's blob object, you can use git cat-file with the -p (pretty-print) flag. First, get the blob hash from git ls-tree, then run:
git cat-file -p <blob-hash>
The first line of output will show the mode, like:
mode 100755 blob a1b2c3d...
3. Check permission changes with git show
As you mentioned, git show can display permission changes between commits. If you want to see when the executable permission was added or removed, run:
git show <commit-hash> -- <file-path>
Look for lines like:
diff --git a/scripts/deploy.sh b/scripts/deploy.sh
old mode 100644
new mode 100755
This tells you that the commit in question changed the file from non-executable to executable.
Note for the current working directory
If you just want to check the local file's permissions (not the version stored in Git), you can use your system's ls -l command:
ls -l <file-path>
But keep in mind: local permissions might differ from what's in Git if you modified them without committing the change. Always use git ls-tree HEAD -- <file-path> to confirm the version tracked in the current commit.
内容的提问来源于stack exchange,提问作者Keith Thompson

