77

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

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
ylluminate
  • 12,102
  • 17
  • 78
  • 152

4 Answers4

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

l0b0
  • 55,365
  • 30
  • 138
  • 223
Borealid
  • 95,191
  • 9
  • 106
  • 122
  • 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 '12 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 '12 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 02 '13 at 01:44
  • full list of commits `git log --full-history --date=iso |grep Date` – Aquarius Power Aug 23 '14 at 01: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` – Jonathan Stray May 26 '16 at 02:32
  • @JonathanStray maybe make that an answer? – jthill Jun 24 '16 at 13:02
  • Nice. Just to chime in a bit, I was interested in diffing my current, changed, file to see how my current round of work had changed it and `git diff 'HEAD@{3 weeks ago}' -- mydir/myfile` did the trick perfectly. – JL Peyret Aug 07 '20 at 21:19
26

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
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
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
S.B
  • 13,077
  • 10
  • 22
  • 49
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
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()
Allen
  • 11
  • 2