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

后端 未结 8 1968
眼角桃花
眼角桃花 2020-11-27 12:25

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 cert

相关标签:
8条回答
  • 2020-11-27 12:37
    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
    
    0 讨论(0)
  • 2020-11-27 12:39

    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.

    0 讨论(0)
  • 2020-11-27 12:56

    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

    0 讨论(0)
  • 2020-11-27 12:56

    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.

    0 讨论(0)
  • 2020-11-27 12:58

    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.

    0 讨论(0)
  • 2020-11-27 12:58

    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

    0 讨论(0)
提交回复
热议问题