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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6795070/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:24:04  来源:igfitidea点击:

Is there a way in git to obtain a push date for a given commit?

gitgithubgit-push

提问by justkikuchi

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.

我想知道是否有办法查看与 git 日志中的每个提交相关联的推送日期。如果这是不可能的,有没有办法查看某个推送下的所有提交。

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.

我编写了一个程序,需要在提交时跟踪提交。因为 git log 是按提交日期而不是推送日期排序的,所以我看不到推送的最新提交。例如,如果用户在推送到 master 之前 2 天提交到他的本地存储库,则该提交将被放置在 master 存储库日志中其他提交的 2 天之后。

回答by MarcH

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.reflogExpireand gc.reflogExpireUnreachable. Check git help reflogfor details and to understand how and why this works.

警告:你可能要重写的默认值gc.reflogExpiregc.reflogExpireUnreachable。检查git help reflog详细信息并了解其工作原理和原因。

The two commands above must be run inside the clone you push to. If that is not possible then an approximationis to run in another, permanentclone:

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

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

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

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

回答by Richard Hansen

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?

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

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.

不幸的是,无法将日期附加到提交消息中。这会改变提交 ID(它是内容的 SHA1 哈希),导致各种问题。

The good news

好消息

Fortunately, Git has a (relatively new) feature called notes. This feature allows you to attach arbitrary text to commits, which git logcan display. Notes can be edited and shared with others.

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

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 notesfor details.

详情请参阅git help notes

How to record the date

如何记录日期

Here's the approach I recommend:

这是我推荐的方法:

  1. Modify the post-receivehook 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 receivehook 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.

  1. 修改post-receive共享存储库上的钩子,为每个更新的引用遍历每个新可达的提交。
  2. 对于每个提交,在提交的注释中附加类似“[repository_url] 的 [user] 将此提交添加到 [ref] on [date]”之类的内容。

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

  3. 修改您的receive钩子以拒绝更新您的笔记参考(以防止用户意外或故意弄乱笔记)。
  4. 告诉所有用户从他们的工作树中运行以下命令:

    # 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/*'
    

    这一步是必要的,因为 Git 默认忽略上游存储库中的非分支、非标签引用。

The above assumes that references are only advanced, never deleted or force-updated. You'll probably want to have the post-receivehook also append "removed on [date]" notes to handle these cases.

以上假设引用仅是高级的,从不删除或强制更新。您可能希望post-receive挂钩还附加“在 [日期] 删除”的注释来处理这些情况。

回答by V.C.

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.

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

Note, if you fetched from origin/master, it will print the date you fetched it; NOT the date someone else pushed the commit.

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

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

奖励:你当然可以做更多漂亮的格式化。到目前为止,我喜欢这种颜色编码。不过打字有点多。这会将 SHA 输出为红色,将 reflog 选择器输出为青色,并将 reflog 输出为绿色。

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

回答by Karl Bielefeldt

Take a look at git reflog show master. Probably not the exact format you want, but should point you in the right direction.

看看git reflog show master。可能不是您想要的确切格式,但应该为您指明正确的方向。

Another idea is running a script inside a push hook.

另一个想法是在推钩内运行脚本。

回答by Jonathan Day

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

这个关于检查远程上的 reflog 的答案可能会有所帮助(https://stackoverflow.com/a/8791295/336905),它为您提供有关哪个分支被推送的信息,即使它没有显示哪些提交被推送,但你可以通过查找本地提交日期之后的下一个推送来进行互相关。不是万无一失,但如果您还没有实施@RichardHansen 之前发布的出色笔记建议,则很方便

回答by Rich

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仓库中的“objects”目录中查看commit对象文件的文件修改时间。

回答by dnozay

Why git AuthorDate is different from CommitDate?

为什么 git AuthorDate 与 CommitDate 不同?

  • AuthorDateis when the commit was first created.
  • CommitDateis when the commit was last modified (e.g. rebase).
  • AuthorDate是首次创建提交的时间。
  • CommitDate是上次修改提交的时间(例如变基)。

You can get those with the --prettyformatting options:

您可以使用--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

So, if you and other developers are doing git rebasebefore git push, you will end up with a commit datethat is later than the author date.

所以,如果你和其他开发商都在做git rebase之前git push,你会最终有一个提交日期是晚于作者日期

This command shows the commit date: git log --pretty=fuller

此命令显示提交日期: git log --pretty=fuller

回答by Pashh PGH

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

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