2012-03-11 234 views
39

我习惯于运行一个git的比较,这将使当地git的转速喜欢比较:git diff日期?

git diff HEAD HEAD~110 -- some/file/path/file.ext 

是否有可能使用的日期呢?如果是这样,怎么样?我希望能够在上述示例中插入代替“110”的日期,例如“2012年12月4日”。

回答

51
git diff HEAD '[email protected]{3 weeks ago}' -- some/file/path/file.ext 

严格来说,这并非三周前的修订。相反,它的位置HEAD是在现在的三周前。但它可能足够接近你的目的 - 如果当前分支的HEAD稳步前进,这将是非常准确的,这是大多数人倾向于做的。您可以使用分支名称而不是HEAD来提高准确性。

也可以使用日期/时间,而不是现在的偏移量,如[email protected]{1979-02-26 18:30:00}。见git help rev-parse

+0

在ZSH上,我遇到了'zsh:parse error near \'}''的错误。“有关可能会发生什么的任何想法? – ylluminate 2012-03-11 20:05:29

+3

'zsh'正试图为您解读大括号。引用它们(在整个事物中使用双引号,或者在每个大括号之前使用反斜杠,或者其他)。 – torek 2012-03-11 20:14:20

+0

对,谢谢@torek。 – ylluminate 2012-03-12 21:19:00

1

结合Jonathan Stray's suggestion to use git-rev-list --before在给定日期找到修订和Show just the current branch in Git

#!/bin/sh 
if [ $# -eq 0 ] || [ "$1" = "--help" ]; then 
    cat <<EOF 
Usage: $0 DATE FILE... 
git diff on FILE... since the specified DATE on the current branch. 
EOF 
    exit 
fi 

branch1=$(git rev-parse --abbrev-ref HEAD) 
revision1=$(git rev-list -1 --before="$1" "$branch1") 
shift 

revision2=HEAD 

git diff "$revision1" "$revision2" -- "[email protected]" 

调用此脚本日期和任选的一些文件名,例如

git-diff-since yesterday 
git-diff-since '4 Dec 2012' some/file/path/file.ext 
2

你想要的就是这个。

git diff HEAD '@{3 weeks ago}' -- some/file/path/file.ext 

你应该@{3 weeks ago},不[email protected]{3 weeks ago}比较。

有什么区别?

如果你3周前在另一个分支上,[email protected]{3 weeks ago}会指向分支的HEAD,另一方面@{3 weeks ago}会指向当前分支的HEAD。

您也可以明确指定分支名称。

git diff HEAD '[email protected]{3 weeks ago}' -- some/file/path/file.ext