没有执行此操作的 Git 命令,但您可以在 Bash(或您选择的 shell)中编写脚本:
您可以创建一个执行此操作的脚本:
#!/bin/bash
# Create a file which is a version of your file of interest before last commit
git --no-pager show HEAD~:<file> > original
# Get the number of lines for that file and call it n:
n=$(cat original | wc -l)
# Test whether the last commit modified the file other than
# by adding content at the end
[[ "$(diff <(cat original) <(head -$n <file>))" == "" ]]
# Finally, clean up by deleting the temp file:
rm original
如果文件仅在最后(或根本没有)被修改,这将返回 0,如果您想要阻止修改(即不在最后),则返回 1。
当然,您需要将<file> 替换为您感兴趣的文件的路径。
如果您感兴趣的 2 次提交不是最后 2 次提交,您可以将脚本修改为更通用。例如,在这里,我将使用 <hash1> 和 <hash2> 进行 2 次提交,但您可以使用任何便于识别任何 2 次提交的参考:
#!/bin/bash
# Create a file which is a version of your file of interest at the first commit
# you care about (using its hash):
git --no-pager show <hash1>:<file> > original
# Create a file which is a version of your file of interest at the second commit
# you care about (using its hash):
git --no-pager show <hash2>:<file> > next
# Get the number of lines for the first of these files and call it n:
n=$(cat original | wc -l)
# Test whether the commit with hash2 modified the file other
# than by adding content at the end
[[ "$(diff <(cat original) <(head -$n next))" == "" ]]
# Finally, clean up by deleting the 2 temp files:
rm original next
或者,为了方便起见,因为您可能需要经常在管道中对此进行测试,您可以创建一个函数并将其添加到您的 .bashrc 或等效项中:
function check {
local n
git --no-pager show "$2":"$1" > original
git --no-pager show "$3":"$1" > next
n=$(cat original | wc -l)
[[ "$(diff <(cat original) <(head -$n next))" == "" ]]
rm original next
}
然后您可以将其用于:
check <file-path> <commit-hash1> <commit-hash2>