Alex Rivera | Logout

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

Asked 2011-07-22T19:29:04.570
122

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.

Edit
Report

1 Answer

41

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

answered 2011-07-23T07:42:03.850

Your Answer