How to properly use `git reflog --since=…`?

2019-04-12 19:23发布

I have a repository for which a regular git reflog --date=iso shows a lot of entries, for example see this fragment https://gist.github.com/FreddieChopin/0206c9ef530a056c624b065eed048c9d

As you may notice, there are reflogs for 19th, 22nd, 23rd, 24th, 25th and 26th of February.

But if I would like to limit the output to certain dates, this doesn't work as expected. For example git reflog --date=iso --since="2017-02-20" gives only this https://gist.github.com/FreddieChopin/fb7619dee8fde055a1cce6f6ff2f6eb6 - it stops at "52896f49 HEAD@{2017-02-24 20:53:29 +0100}", even though there are reflogs since 20th of February before that. There are even reflogs for 24th with smaller hours, so I have no idea why it stops exactly there.

The same problem is with another repository I've checked, so this seems to be related to the reflog itself, not the particular repository. The problem with the other repo is even weirder, as for example git reflog --since="50.weeks" gives my commits from the last several days, while git reflog --since="60.weeks" starts to go further back in time - in that repo there are also regular commits since a few years back.

On the other hand, git log --since=... works exactly as expected, so I'm not sure what's the problem here...

1条回答
来,给爷笑一个
2楼-- · 2019-04-12 20:02

Note that Git 2.14.x/2.15 has fixed some issues with reflog.
See commit de23944, commit d08565b, commit 7f97de5, commit 7c2f08a, commit f35650d, commit 82fd0f4, commit 7cf686b (07 Jul 2017), and commit 822601e (09 Jul 2017) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 3ab01ac, 11 Aug 2017)

reflog-walk: apply --since/--until to reflog dates

When doing a reflog walk, we use the commit's date to do any date limiting. In earlier versions of Git, this could lead to nonsense results, since a skipped commit would truncate the traversal.
So a sequence like:

git commit ...
git checkout week-old-branch
git checkout -
git log -g --since=1.day.ago

would stop at the week-old-branch, even though the "git commit" entry further back is still interesting.

As of the prior commit, which uses a parent-less traversal of the reflog, you get the whole reflog minus any commits whose dates do not match the specified options.
This is arguably useful, as you could scan the reflogs for commits that originated in a certain range.

But more likely a user doing a reflog walk wants to limit based on the reflog entries themselves.
You can simulate --until with:

git log -g @{1.day.ago}

but there's no way to ask Git to traverse only back to a certain date. E.g.:

# show me reflog entries from the past day
git log -g --since=1.day.ago

This patch teaches the revision machinery to prefer the reflog entry dates to the commit dates when doing a reflog walk.
Technically this is a change in behavior that affects plumbing, but the previous behavior was so buggy that it's unlikely anyone was relying on it.

A new series of tests has been added for reflog-walk.

查看更多
登录 后发表回答