git diff on date?

Git

Git Problem Overview


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".

Git Solutions


Solution 1 - Git

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 - Git

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 - Git

Combining Jonathan Stray's suggestion to use git-rev-list --before to find the revision at a given date and https://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git/1418022#1418022:

#!/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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionylluminateView Question on Stackoverflow
Solution 1 - GitBorealidView Answer on Stackoverflow
Solution 2 - GitSanghyun LeeView Answer on Stackoverflow
Solution 3 - GitGilles 'SO- stop being evil'View Answer on Stackoverflow