git 有没有办法获取给定提交的推送日期?
我想知道是否有一种方法可以查看与 git 日志中每个提交相关的推送日期。如果这是不可能的,有没有办法查看某个推送下的所有提交。
我编写了一个程序,需要在推送时跟踪提交。因为 git 日志是按提交日期而不是推送日期排序的,所以我无法看到推送的最新提交。例如,如果用户在推送到主存储库前 2 天提交到其本地存储库,则该提交将被放置在主存储库日志中其他提交的 2 天之后。
I am wondering if there is a way to view a push date associated with each commit in the git log. If that is not possible, is there a way to see all the commits under a certain push.
I writing a program that needs to keep track of the commits as they are pushed. Because the git log is ordered by the commit date, not the push date, I am not able to see the most recent commits that are pushed. For example, if a user commits to his local repository 2 days before he pushes to the master, that commit will be placed behind 2 days of other commits in the master repository log.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我花了非常长的时间来收集分散的信息并最终找到这个问题的最佳答案,但现在我知道我已经找到了。只需两行,没有代码,也没有钩子:
终于简单了。
警告:您可能想要覆盖
gc.reflogExpire
和gc.reflogExpireUnreachable
的默认值。检查 git help reflog 获取详细信息并了解其工作原理和原因。上面的两个命令必须在您推送到的克隆内运行。如果不可能,则在另一个永久克隆中运行近似:
切勿删除此永久克隆,否则您将丢失日期。
It took me an insanely long time to gather scattered information and finally find the very best answer to this question, but now I know I have it. In just two lines, no code and no hooks:
Simple at last.
Warning: you probably want to override the default values of
gc.reflogExpire
andgc.reflogExpireUnreachable
. Checkgit help reflog
for details and to understand how and why this works.The two commands above must be run inside the clone you push to. If that is not possible then an approximation is to run in another, permanent clone:
Never delete this permanent clone or you will lose the dates.
Git 是一个分布式版本控制系统,因此您必须仔细定义“推送日期”的含义。例如,假设用户 A 将一些提交推送到用户 B 的存储库。稍后,用户 B 将这些相同的提交推送到第三个存储库。您对哪个日期感兴趣?
我推测您有一个共享存储库,并希望该共享存储库的用户能够确定某些内容何时发布到存储库。如果这是真的,您将必须在共享存储库中收集该信息。
坏消息
不幸的是,没有办法将日期附加到提交消息中。这会更改提交 ID(内容的 SHA1 哈希值),从而导致各种问题。
好消息
幸运的是,Git 有一个(相对较新的)功能,称为注释。此功能允许您将任意文本附加到提交,
git log
可以显示这些文本。可以编辑笔记并与他人共享。您可以使用注释功能将“此提交于 [日期] 收到”消息附加到共享存储库收到的每个提交。
有关详细信息,请参阅
git 帮助说明
。如何记录日期
这是我推荐的方法:
post-receive
挂钩,以遍历每个更新的引用的每个新可访问的提交。对于每个提交,请在提交注释中附加类似“[repository_url] 的[用户] 在 [日期] 将此提交添加到 [参考]”的内容。
您可能希望使用专用于此目的的注释引用(例如
refs/notes/received-on
),而不是默认的refs/notes/commits
。这将防止与为其他目的创建的注释发生冲突。receive
挂钩以拒绝对笔记引用的更新(以防止用户意外或故意弄乱笔记)。告诉所有用户从其工作树内部运行以下命令:
此步骤是必要的,因为默认情况下 Git 会忽略上游存储库中的非分支、非标记引用。
上面假设引用只是高级的,从未被删除或强制更新。您可能希望让
post-receive
挂钩还附加“已于 [日期] 删除”注释来处理这些情况。Git is a distributed version control system, so you have to carefully define what you mean by "push date". For example, suppose user A pushes some commits to user B's repository. Some point later, user B pushes those same commits to a third repository. Which date are you interested in?
I'm speculating that you have a shared repository and want the users of that shared repository to be able to determine when something was published to the repository. If that's true, you'll have to collect that information in the shared repository.
The bad news
Unfortunately, there's no way to append the date to the commit messages. That would change the commit ID (which is a SHA1 hash of the contents), causing all sorts of problems.
The good news
Fortunately, Git has a (relatively new) feature called notes. This feature allows you to attach arbitrary text to commits, which
git log
can display. Notes can be edited and shared with others.You can use the notes feature to attach a "this commit was received on [date]" message to each commit as it is received by the shared repository.
See
git help notes
for details.How to record the date
Here's the approach I recommend:
post-receive
hook on your shared repository to walk each newly reachable commit for each updated reference.For each commit, append something like "[user] of [repository_url] added this commit to [ref] on [date]" to the commit's note.
You may want to use a notes ref dedicated to this purpose (like
refs/notes/received-on
) instead of the defaultrefs/notes/commits
. This will prevent conflicts with notes created for other purposes.receive
hook to deny updates to your notes reference (to keep users from accidentally or purposely messing with the notes).Tell all the users to run the following commands from inside their working tree:
This step is necessary because Git ignores non-branch, non-tag references in upstream repositories by default.
The above assumes that references are only advanced, never deleted or force-updated. You'll probably want to have the
post-receive
hook also append "removed on [date]" notes to handle these cases.这似乎对我来说效果很好。提交者日期 (%cd) 具有误导性,因为它不一定与推送日期相同。然而,--date=iso 选项将输出推送/获取日期。
请注意,如果您从 origin/master 获取,它将打印您获取的日期;不是其他人推送提交的日期。
额外奖励:您当然可以进行更漂亮的格式化。到目前为止,我喜欢这种颜色编码。不过打字有点多。这会将 SHA 输出为红色,将引用日志选择器输出为青色,将引用日志输出为绿色。
This seems to work pretty well for me. The committer date (%cd) is misleading because it's not necessarily the same as push date. The --date=iso option, however will output the push/fetch date.
Note, if you fetched from origin/master, it will print the date you fetched it; NOT the date someone else pushed the commit.
Bonus: You can of course, do more pretty formatting. So far, I like this color coding. It's a bit much to type though. This will output the SHA to red, reflog selector to cyan, and reflog subject to green.
这个关于检查远程引用日志的答案可能会有所帮助(https://stackoverflow.com/a/8791295/336905)通过向您提供有关推送了哪个分支的信息,即使它不显示推送了哪些提交,但您可以通过查找本地提交日期之后的下一个推送来进行交叉关联。虽然不是万无一失,但如果您还没有实施之前发布的 @RichardHansen 的出色注释建议,那么会很方便
This answer regarding inspecting the reflog on the remote might help (https://stackoverflow.com/a/8791295/336905) by giving you information on which a branch was pushed even through it doesn't show which commits were pushed, but you could cross-correlate by finding the next push after the local commit date. Not fool-proof, but handy if you haven't already implemented the excellent notes suggestion from @RichardHansen posted earlier
看看
git reflog show master
。可能不是您想要的确切格式,但应该为您指明正确的方向。另一个想法是在推钩内运行脚本。
Take a look at
git reflog show master
. Probably not the exact format you want, but should point you in the right direction.Another idea is running a script inside a push hook.
您还可以在服务器本身的 git 存储库的“objects”目录中查看提交对象文件的文件修改时间。
You can also look at the file modification time of the commit object file in the "objects" directory in the git repository on the server itself.
为什么 git AuthorDate 与 CommitDate 不同?
AuthorDate 是第一次创建提交的时间。
CommitDate
是上次修改提交的时间(例如变基)。您可以使用
--pretty
格式选项获取这些内容:因此,如果您和其他开发人员在
git push
之前执行git rebase
,您最终的提交日期将晚于作者日期。
此命令显示提交日期:
git log --pretty=fuller
Why git AuthorDate is different from CommitDate?
AuthorDate
is when the commit was first created.CommitDate
is when the commit was last modified (e.g. rebase).You can get those with the
--pretty
formatting options:So, if you and other developers are doing
git rebase
beforegit push
,you will end up with a commit date that is later than the author date.
This command shows the commit date:
git log --pretty=fuller
我想您可以使用下一个符号来获取推送日期:
git log -g --date=本地
I guess you can use next notation to obtain the push date:
git log -g --date=local
试试这个。
Try this.