除了编写别名或脚本之外,是否还有更短的命令来获取特定提交的差异?

git diff 15dc8^..15dc8

如果你只给一个提交id git diff 15dc8,它就会对HEAD进行不同的提交。


当前回答

git diff 15dc8 15dce~1

~1表示“父母”,~2表示“祖父母”,等等。

其他回答

这使用别名,所以它不能准确地回答你的问题,但我发现这些对你想做的事情很有用……

alias gitdiff-1="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 2|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git diff"
alias gitdiff-2="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 3|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git diff"
alias gitdiff-3="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 4|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git diff"

alias gitlog-1="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 2|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git log --summary"
alias gitlog-2="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 3|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git log --summary"
alias gitlog-3="git log --reverse|grep commit|cut -d ' ' -f2|tail -n 4|head -n 2|xargs echo|sed -e 's/\s/../'|xargs -n 1 git log --summary"

如果你知道多久以前,你可以试试这样的方法:

# Current branch vs. parent
git diff HEAD^ HEAD

# Current branch, diff between commits 2 and 3 times back
git diff HEAD~3 HEAD~2

预先提交的工作原理是这样的:

# Parent of HEAD
git show HEAD^1

# Grandparent
git show HEAD^2

有很多方法可以指定提交:

# Great grandparent
git show HEAD~3

详情请参阅本页。

许多提到的例子(例如git diff 15dc8^!,或git diff 15dc8^..15dc8)不工作,如果你正在使用Z shell和extendedglob选项集。你可以通过以下三种方法之一来修复它:

Unsetopt extendedglob(和/或从.zshrc中删除它) setopt NO_NOMATCH(和/或在.zshrc中设置它) 每次使用反斜杠转义插入符号和砰砰声,例如,git diff 15dc8\^\!

git diff 15dc8 15dce~1

~1表示“父母”,~2表示“祖父母”,等等。

使用git show $COMMIT。它将向您显示提交的日志消息,以及特定提交的差异。