Is there a way in git to obtain a push date for a given commit?
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.
7 Solutions collect form web for “Is there a way in git to obtain a push date for a given commit?”
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
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.
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.
git help notes for details.
How to record the date
Here’s the approach I recommend:
- Modify the
post-receivehook on your shared repository to walk each newly reachable commit for each updated reference.
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.
- Modify your
receivehook to deny updates to your notes reference (to keep users from accidentally or purposely messing with the notes).
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.
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.
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
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.
Why git AuthorDate is different from CommitDate?
AuthorDateis when the commit was first created.
CommitDateis 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
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
I guess you can use next notation to obtain the push date:
git log -g –date=local