git 有没有办法获取给定提交的推送日期?

发布于 2024-11-25 10:57:17 字数 198 浏览 1 评论 0原文

我想知道是否有一种方法可以查看与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

输什么也不输骨气 2024-12-02 10:57:17

我花了非常长的时间来收集分散的信息并最终找到这个问题的最佳答案,但现在我知道我已经找到了。只需两行,没有代码,也没有钩子:

# required for a bare repo
git config core.logAllRefUpdates true

git reflog --date=local master

终于简单了。

警告:您可能想要覆盖 gc.reflogExpiregc.reflogExpireUnreachable 的默认值。检查 git help reflog 获取详细信息并了解其工作原理和原因。

上面的两个命令必须在您推送到的克隆内运行。如果不可能,则在另一个永久克隆中运行近似

git fetch               origin        # often and *regularly*
git reflog --date=local origin/master

切勿删除此永久克隆,否则您将丢失日期。

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:

# required for a bare repo
git config core.logAllRefUpdates true

git reflog --date=local master

Simple at last.

Warning: you probably want to override the default values of gc.reflogExpire and gc.reflogExpireUnreachable. Check git 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:

git fetch               origin        # often and *regularly*
git reflog --date=local origin/master

Never delete this permanent clone or you will lose the dates.

浮世清欢 2024-12-02 10:57:17

Git 是一个分布式版本控制系统,因此您必须仔细定义“推送日期”的含义。例如,假设用户 A 将一些提交推送到用户 B 的存储库。稍后,用户 B 将这些相同的提交推送到第三个存储库。您对哪个日期感兴趣?

我推测您有一个共享存储库,并希望该共享存储库的用户能够确定某些内容何时发布到存储库。如果这是真的,您将必须在共享存储库中收集该信息。

坏消息

不幸的是,没有办法将日期附加到提交消息中。这会更改提交 ID(内容的 SHA1 哈希值),从而导致各种问题。

好消息

幸运的是,Git 有一个(相对较新的)功能,称为注释。此功能允许您将任意文本附加到提交,git log 可以显示这些文本。可以编辑笔记并与他人共享。

您可以使用注释功能将“此提交于 [日期] 收到”消息附加到共享存储库收到的每个提交。

有关详细信息,请参阅 git 帮助说明

如何记录日期

这是我推荐的方法:

  1. 修改共享存储库上的 post-receive 挂钩,以遍历每个更新的引用的每个新可访问的提交。
  2. 对于每个提交,请在提交注释中附加类似“[repository_url] 的[用户] 在 [日期] 将此提交添加到 [参考]”的内容。

    您可能希望使用专用于此目的的注释引用(例如refs/notes/received-on),而不是默认的refs/notes/commits。这将防止与为其他目的创建的注释发生冲突。

  3. 修改您的 receive 挂钩以拒绝对笔记引用的更新(以防止用户意外或故意弄乱笔记)。
  4. 告诉所有用户从其工作树内部运行以下命令:

    # 从共享存储库中获取所有笔记。
    # 假设共享存储库远程名称为“origin”。
    git config --add remote.origin.fetch '+refs/notes/*:refs/remote-notes/origin/*'
    
    # 运行“git log”时显示共享存储库中的所有注释
    git config --add Notes.displayRef 'refs/remote-notes/origin/*'
    

    此步骤是必要的,因为默认情况下 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:

  1. Modify the post-receive hook on your shared repository to walk each newly reachable commit for each updated reference.
  2. 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 default refs/notes/commits. This will prevent conflicts with notes created for other purposes.

  3. Modify your receive hook to deny updates to your notes reference (to keep users from accidentally or purposely messing with the notes).
  4. Tell all the users to run the following commands from inside their working tree:

    # Fetch all notes from the shared repository.
    # Assumes the shared repository remote is named 'origin'.
    git config --add remote.origin.fetch '+refs/notes/*:refs/remote-notes/origin/*'
    
    # Show all notes from the shared repository when running 'git log'
    git config --add notes.displayRef 'refs/remote-notes/origin/*'
    

    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.

安稳善良 2024-12-02 10:57:17
git reflog show origin/master --pretty='%h %gd %gs %s' --date=iso

这似乎对我来说效果很好。提交者日期 (%cd) 具有误导性,因为它不一定与推送日期相同。然而,--date=iso 选项将输出推送/获取日期

请注意,如果您从 origin/master 获取,它将打印您获取的日期;不是其他人推送提交的日期。

 - %h:  abrev. hash
 - %gd: human readable reflog selector
 - %gs: reflog subject
 - %s:  subject/commit message

额外奖励:您当然可以进行更漂亮的格式化。到目前为止,我喜欢这种颜色编码。不过打字有点多。这会将 SHA 输出为红色,将引用日志选择器输出为青色,将引用日志输出为绿色。

git reflog show origin/master --pretty='format:%C(red)%h%Creset %C(cyan)%gd%Creset %C(green)%gs%Creset: %s' --date=iso
git reflog show origin/master --pretty='%h %gd %gs %s' --date=iso

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.

 - %h:  abrev. hash
 - %gd: human readable reflog selector
 - %gs: reflog subject
 - %s:  subject/commit message

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.

git reflog show origin/master --pretty='format:%C(red)%h%Creset %C(cyan)%gd%Creset %C(green)%gs%Creset: %s' --date=iso
聚集的泪 2024-12-02 10:57:17

这个关于检查远程引用日志的答案可能会有所帮助(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

你在我安 2024-12-02 10:57:17

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

蓝眸 2024-12-02 10:57:17

您还可以在服务器本身的 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.

愛上了 2024-12-02 10:57:17

为什么 git AuthorDate 与 CommitDate 不同?

  • AuthorDate 是第一次创建提交的时间。
  • CommitDate 是上次修改提交的时间(例如变基)。

您可以使用 --pretty 格式选项获取这些内容:

       o    %cd: committer date
       o    %cD: committer date, RFC2822 style
       o    %cr: committer date, relative
       o    %ct: committer date, UNIX timestamp
       o    %ci: committer date, ISO 8601 format

因此,如果您和其他开发人员在 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:

       o    %cd: committer date
       o    %cD: committer date, RFC2822 style
       o    %cr: committer date, relative
       o    %ct: committer date, UNIX timestamp
       o    %ci: committer date, ISO 8601 format

So, if you and other developers are doing git rebase before git 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

暮色兮凉城 2024-12-02 10:57:17

我想您可以使用下一个符号来获取推送日期:
git log -g --date=本地

I guess you can use next notation to obtain the push date:
git log -g --date=local

被翻牌 2024-12-02 10:57:17

试试这个。

git log {hash} -1 --format=%cd --date=local 

Try this.

git log {hash} -1 --format=%cd --date=local 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文