79

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

4 Answers 4

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

7
  • On ZSH I'm getting a failure of: zsh: parse error near `}' Any ideas as to what may be going on there?
    – ylluminate
    Mar 11, 2012 at 20:05
  • 3
    zsh is trying to interpret the braces for you. Quote them (double quotes around the whole thing, or backslash before each brace, or whatever).
    – torek
    Mar 11, 2012 at 20:14
  • 2
    Well, this doesn't really seem to work if you have a fresh checkout of a repository, my understanding is that the feature you quote is based on git-reflog, not on git-log.
    – cnst
    Dec 2, 2013 at 1:44
  • full list of commits git log --full-history --date=iso |grep Date Aug 23, 2014 at 1:00
  • 8
    As @cnst notes, this won't work with a fresh checkout. It will give the error "warning: Log for 'HEAD' only goes back to...". To resolve this, you can figure out the SHA you want to compare using like git rev-list -1 --before="2016-03-27 12:00" master May 26, 2016 at 2:32
27

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
11

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
1

This command may help you.

git log --since="1 weeks ago" --name-only --pretty=format: some/file/path/

Or

import subprocess  
current_branch = subprocess.check_output(
    "git rev-parse --abbrev-ref HEAD", shell=True).strip()
revision1 = subprocess.check_output(
    "git rev-list -1 --before='{duration} ago' '{branch}' --".format(
        duration=duration, branch=current_branch), shell=True).strip()
content = subprocess.check_output(
    "git diff --name-only '{}' HEAD -- some/file/path".format(revision1), shell=True).strip()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.