如何使用 Git 获取两个日期之间发生的所有提交之间的差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1161609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the diff between all the commits that occurred between two dates with Git?
提问by Chris
Or just all the commits that occurred between two dates? In SVN, you could do something like
或者只是两个日期之间发生的所有提交?在 SVN 中,你可以做类似的事情
svn diff -r{date}:{date}
to do it! I can't seem to find a Git equivalent to this.
去做吧!我似乎找不到与此等效的 Git。
Specifically I'm looking at writing a script to send out daily emails with all the code committed that day and by who.
具体来说,我正在考虑编写一个脚本来发送每日电子邮件,其中包含当天提交的所有代码以及由谁提交。
回答by seth
回答by Weidenrinde
The previous suggestions have some drawbacks. Basically, I was looking for something equivalent to cvs diff -D"1 day ago" -D"2010-02-29 11:11"
. While collecting more and more information, I found a solution.
之前的建议有一些缺点。基本上,我正在寻找与cvs diff -D"1 day ago" -D"2010-02-29 11:11"
. 在收集越来越多的信息的同时,我找到了解决方案。
Things I have tried:
我尝试过的事情:
git whatchanged --since="1 day ago" -p
from hereBut this gives a diff for each commit, even if there are multiple commits in one file. I know that "date" is a bit of a loose concept in git, I thought there must be some way to do this.
git diff 'master@{1 day ago}..master
gives some warningwarning: Log for 'master' only goes back to Tue, 16 Mar 2010 14:17:32 +0100.
and does not show all diffs.git format-patch --since=yesterday --stdout
does not give anything for me.revs=$(git log --pretty="format:%H" --since="1 day ago");git diff $(echo "$revs"|tail -n1) $(echo "$revs"|head -n1)
works somehow, but seems complicated and does not restrict to the current branch.
git whatchanged --since="1 day ago" -p
从这里但这会为每个提交提供一个差异,即使一个文件中有多个提交。我知道“日期”在 git 中有点松散的概念,我认为必须有某种方法可以做到这一点。
git diff 'master@{1 day ago}..master
给出一些警告warning: Log for 'master' only goes back to Tue, 16 Mar 2010 14:17:32 +0100.
并且不显示所有差异。git format-patch --since=yesterday --stdout
不给我任何东西。revs=$(git log --pretty="format:%H" --since="1 day ago");git diff $(echo "$revs"|tail -n1) $(echo "$revs"|head -n1)
以某种方式工作,但看起来很复杂并且不限于当前分支。
Finally:
最后:
git diff $(git rev-list -n1 --before="1 day ago" master)
seems to work and a default way to do similar things, although more complicated than I thought.
Funnily, git-cvsserver does not support "cvs diff -D" (without that it is documented somewhere).
有趣的是,git-cvsserver 不支持“cvs diff -D”(没有在某处记录)。
回答by CB Bailey
"date" is a bit of a loose concept in git. A commit will have an author date that may be some time well in the past before someone actually pulls/commits the commit into their repository, also the commit may be rebased and updated to be on top of an apparently newer commit.
“日期”在 git 中有点松散的概念。提交将有一个作者日期,该日期可能早在有人实际将提交拉入/提交到他们的存储库之前的某个时间,并且提交也可能被重新定位并更新到明显较新的提交之上。
A commit also has an commit date which is updated if a commit is rebased or amended in any way. These commits are more likely to be in some sort of chronological order but you are still at the mercy of the committer having the correct time set on his computer and even so, an unmodified commit can sit on a feature branch on a remote repository indefinitely before being merged into the master branch of a central repository.
提交还有一个提交日期,如果提交以任何方式重新定位或修改,则该日期会更新。这些提交更有可能按某种时间顺序排列,但您仍然受制于在其计算机上设置正确时间的提交者的摆布,即便如此,未修改的提交可以无限期地放在远程存储库的功能分支上被合并到中央存储库的主分支中。
What is probably most useful for your purposes is the reflog date on the particular repository in question. If you have per-branch reflogs enabled (see git config core.logAllRefUpdates
) then you can use the ref@{date}
syntax to refer to where a branch was at a particular time.
可能对您的目的最有用的是相关特定存储库的 reflog 日期。如果您启用了每个分支的引用日志(请参阅 参考资料git config core.logAllRefUpdates
),那么您可以使用ref@{date}
语法来引用分支在特定时间的位置。
E.g.
例如
git log -p master@{2009-07-01}..master@{now}
You can also use 'fuzzy' descriptions like:
您还可以使用“模糊”描述,例如:
git log -p "master@{1 month ago}..master@{yesterday}"
These commands will show all commits that have 'appeared' in the given branch of the repository regardless of how 'old' they actually are according to their author and commit dates.
这些命令将根据作者和提交日期显示在存储库的给定分支中“出现”的所有提交,而不管它们实际有多“旧”。
Note that the per-branch reflog is specific to a repository, so if you're running the log command on a clone, and you don't pull for (say) a month then pull all the changes for the last month at once, then all of the last month's changes will appear in a @{1 hour ago}..@{now}
range. If you are able to run the log command on the 'central' repostory that people push to, then it may do what you want.
请注意,每个分支的 reflog 特定于存储库,因此如果您在克隆上运行 log 命令,并且您没有拉取(例如)一个月,那么一次拉取上个月的所有更改,那么上个月的所有变化都会出现在一个@{1 hour ago}..@{now}
范围内。如果您能够在人们推送到的“中央”存储库上运行 log 命令,那么它可能会执行您想要的操作。
回答by AA.
git diff --stat @{2013-11-01}..@{2013-11-30}
or
或者
git diff --stat @{2.weeks.ago}..@{last.week}
回答by Jakub Nar?bski
Perhaps
也许
$ git format-patch --committer=<who> --since=yesterday --stdout
is what you want (with or without '--stdout')?
是你想要的(有或没有'--stdout')?
回答by Matt McClure
I believe the general solution is to use:
我相信一般的解决方案是使用:
git rev-list -n1 --first-parent --until=<a date string> <a ref>
Without --first-parent, you might get a commit from a branch that was later merged into a ref
but hadn't been merged as of a date string
.
如果没有 --first-parent,您可能会从一个分支获得一个提交,该分支后来被合并到a ref
但尚未合并到a date string
.
Here's an alternative using --children
and grep
instead of -n1
:
这是使用--children
和grep
代替的替代方法-n1
:
mlm_git_ref_as_of() {
# # Examples #
#
# Show all commits between two dates:
#
# git log $(mlm_git_ref_as_of '2012-05-21 09:00:00-0400')..$(mlm_git_ref_as_of '2012-05-21 17:00:00-0400')
#
# Show diffs of all commits between two dates:
#
# git diff $(mlm_git_ref_as_of '2012-05-21 09:00:00-0400')..$(mlm_git_ref_as_of '2012-05-21 17:00:00-0400')
local as_of=""
local ref="${2:-HEAD}"
# Get the most recent commit (--children, grep -v ' ') that was on
# the given branch ($ref, --first-parent) as of a given date
# ($as_of)
git rev-list --children --first-parent --until="$as_of" "$ref" | grep -v ' '
}
I wasn't familiar with git whatchanged
before reading this Q&A, but it gives very different results for me, so I'm not sure what it's doing.
git whatchanged
在阅读此问答之前,我并不熟悉,但它为我提供了非常不同的结果,所以我不确定它在做什么。
回答by Matt McClure
Another simple way that you can get a diff of all changes since a certain date is to simply find the first commit X
that occured on or after that date, then use
获取自某个日期以来所有更改的差异的另一种简单方法是简单地找到X
在该日期或之后发生的第一次提交,然后使用
git diff X
This has the advantage that it doesn't depend on reflog entries in a fresh clone, unlike the
这样做的优点是它不依赖于新克隆中的 reflog 条目,这与
git diff <reference>@{n}..
git log <reference>@{n}..
solutions in
解决方案
回答by Nick Dandoulakis
You can also use git-format-patchto prepare patches (diffs) and send them through email.
您还可以使用git-format-patch准备补丁(差异)并通过电子邮件发送它们。
Use options [since] or [revision range] to specify commits range.
使用选项 [since] 或 [revision range] 指定提交范围。
回答by gahooa
This is more of a funny answer, because there is likely a better way. This will show all commit hashes for today.
这更像是一个有趣的答案,因为可能有更好的方法。这将显示今天的所有提交哈希。
git log --pretty="format:%H %ai" | grep `date +"%Y-%m-%d"` | awk {'print '}`
;·)
;·)
回答by avivamg
In order to watch files changes from date to date on your branch ,use the following formula:
为了在您的分支上观察文件从日期到日期的变化,请使用以下公式:
- checkout your branch.
- pull and update changes from remote repository
- watch diff files from date to date range
- 结帐您的分支机构。
- 从远程存储库中拉取和更新更改
- 从日期到日期范围观察差异文件
example:
例子:
git checkout <branch>
git pull
git diff --stat @{fromDate}..@a{toDate}
Pay attention the dates are on YYYY-MM-DDformat:
注意日期是 YYYY-MM-DD格式:
git diff --stat @{2019-08-20}..@a{2019-08-21}
If you'd like to observe changes on specific file in specific time range(watch diff in code), just navigate the current file:
如果您想观察特定时间范围内特定文件的变化(在代码中观察差异),只需导航当前文件:
example:
例子:
git diff @{2019-01-01}..@{2019-01-02} ~/dev/myApp/package.json