如果我想在两次提交之间看到单个文件中的差异,我可以运行类似
的内容git diff HEAD..HEAD~3 -- file
不幸的是,如果那个特定文件在这些提交之间没有改变,那么该命令就没用了。如果我不想使用gitk
,我必须使用
git log -- pathto/file
然后手动将这些提交传递给git diff
。
是否存在某种技巧,其中包含历史简化以使用某种简化语法(如HEAD~3
)来引用更改该特定文件的第3次提交,而不仅仅是之前的第3次提交?
答案 0 :(得分:1)
我想出了一个名为ndiff
的别名。运行
git ndiff <number> <path>
显示<n>
- 最后一次修订(以拓扑顺序)更改<path>
(可能是文件或目录)之间的差异。要定义此别名,请使脚本可执行,将其放在搜索路径上,然后运行
git config --global alias.ndiff '! git-ndiff'
这是对我的一个存储库中的别名的测试。与存储库中一样,最后三次提交未更改README
文件:
$ git log HEAD~3..HEAD -- README
$
但是,使用我的ndiff
别名,我可以区分最后一次更改README
的提交,如下所示:
$ git ndiff 3 README
diff --git a/README b/README
index d482a72..5b6d0b5 100644
--- a/README
+++ b/README
@@ -10,4 +10,5 @@ LaTeX & friends. Three predefined styles, one of which closely mimicks that
of the Matlab editor, are available and can be invoked by listings macros
and environments in conjunction with (most) options provided by the listings
package. The appearance of your Matlab listings can be further tweaked via a
-key-value interface extending that of listings’.
+key-value interface extending that of listings'. Partial support for Octave
+syntax is provided.
$
快乐的日子!
(该脚本可在GitHub上的Jubobs/git-aliases获得。)
#!/bin/sh
# git-ndiff.sh
#
# Usage: git ndiff <n> <path>
#
# Show changes between the n-th revision that changed <path> and HEAD
#
# To make a Git alias called 'ndiff' out of this script,
# put the latter on your search path, make it executable, and run
#
# git config --global alias.ndiff '! git-ndiff'
ndifferror(){
printf "usage: git ndiff <n> <path>\n"
printf "where <n> is a positive integer\n"
exit 1
}
# Check that two arguments were passed
if [ $# -ne 2 ]; then
ndifferror
fi
# Check that the first argument is a positive integer
if [ "$1" -gt 0 ] >/dev/null 2>&1
then
n=$1
else
ndifferror
fi
# Check that the second argument is an existing path
if [ -e $2 ]; then
path="$2"
else
exit 0
fi
# Get the revision of interest
nthrev=$(git log --follow --pretty=format:%H -- $path | sed -n "$n""p")
# Note: unfortunately, the "--follow" functionality is only provided by the
# porcelain command "git log", and not by any plumbing command (Git 2.3.3)
# Show changes of interest
if [ -n "$nthrev" ]
then
git diff $nthrev -- $path
else
exit 0
fi
exit $?