114

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.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
justkikuchi
  • 1,241
  • 2
  • 9
  • 3

9 Answers9

91

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.

MarcH
  • 18,738
  • 1
  • 30
  • 25
  • 2
    I would like to add that in my case I had to do `git reflog --date=local origin/master` (note `origin/`) to see the list of pushes. Otherwise only commits, checkouts and pulls were in the list (which is useful, too). Actually, I was pointed to it by [@JonathanDay's answer](http://stackoverflow.com/a/14680017/693538). – NIA Mar 22 '13 at 09:50
  • @NIA: origin/master will only give you an approximation. I've updated my answer following your comment, does this clarify? – MarcH Mar 25 '13 at 09: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.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
11
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
V.C.
  • 161
  • 2
  • 8
  • Hi V.C does it mean that if the output of command "reflog show origin/master --pretty='%Cred%h%Creset -%C(yellow)%gd%Creset %Cblue(%gs)%Creset %s' --date=iso" is "da4c192cd -origin/master@{2019-02-07 08:13:40 +0100} (pull: fast-forward) JIRA-2542 enhance test report", it means then content of my branch JIRA-2542 was merged on origin/master through a fast-froward at 2019-02-07 08:13:40? – Simon Feb 07 '19 at 08:05
  • Hi Simon, yes. You did a git pull on your branch and it was merged on origin/master via fast-forward at that time. – V.C. Feb 14 '19 at 16:57
  • It works only on the author's repository. If I clone a repository, the value is empty. You can't see other people's push time. – ramwin Jan 16 '20 at 08:54
  • I can confirm, on fresh clone its empty. – Nikolai Ehrhardt Jul 28 '22 at 18:54
5

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

Community
  • 1
  • 1
Jonathan Day
  • 18,519
  • 10
  • 84
  • 137
  • 2
    With a note that reflog of `origin/branch` will only show the changes made on **current** machine, this is extremely useful! For day-to-day use I don't want to implement any hooks, so for a simple question *"Hmmm, when I pushed that commit last week?"* — it works great. – NIA Mar 22 '13 at 09:54
5

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.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • I thought about running push hook, but it seems like that should be a last ditch effort. With a hook it seems like every user would have to have that hook in each of their repositories. Can you be more specific about the `git reflog show master`? I want to be able to view the push date of every user's individual commits. – justkikuchi Jul 22 '11 at 21:05
  • 1
    git reflog shows the _order_ of changes in whatever repo you run it in. I'm not sure if there's a direct way to get the date, but in `.git/logs/refs/heads/master` it shows a timestamp. – Karl Bielefeldt Jul 22 '11 at 21:46
  • 3
    As for hooks, there are receive hooks that run on the machine you push _to_. Since pushing is only relevant relative to a specific repo, I'm assuming you have a certain "blessed" repo you want to run it on. The same goes for `git reflog` for that matter. – Karl Bielefeldt Jul 22 '11 at 21:48
4

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.

Rich
  • 15,048
  • 2
  • 66
  • 119
4

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

Community
  • 1
  • 1
dnozay
  • 23,846
  • 6
  • 82
  • 104
1

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

Pashh PGH
  • 11
  • 1
0

Try this.

git log {hash} -1 --format=%cd --date=local 
W.Perrin
  • 4,217
  • 32
  • 31