git diff on date?

I'm accustomed to running a git comparison that will allow comparison with local git revs like:

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

Is it possible to use the date instead? And if so, how? I would like to be able insert in place of the "110" in the above example, a date such as "4 Dec 2012".


Solution 1:

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

This is not, strictly speaking, the revision made three weeks ago. Instead, it's the position HEAD was at three weeks prior to the present. But it's probably close enough for your purposes - it will be very accurate if the current branch's HEAD moved forward steadily, as most tend to do. You can improve the accuracy by using a branch name instead of HEAD.

Instead of an offset-from-the-present, you can also use a date/time, like HEAD@{1979-02-26 18:30:00}. See git help rev-parse.

Solution 2:

What you want must be this.

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

You should compare with @{3 weeks ago}, not HEAD@{3 weeks ago}.

What is difference?

If you were on another branch 3 weeks ago, HEAD@{3 weeks ago} would point the HEAD of the branch, on the other hand @{3 weeks ago} would point the HEAD of the current branch.

You can also explicitly name the branch.

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

Solution 3:

Combining Jonathan Stray's suggestion to use git-rev-list --before to find the revision at a given date and 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" -- "$@"

Call this script with a date and optionally some file names, e.g.

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