Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 399
justkikuchi Avatar asked Jul 22 '11 19:07

justkikuchi


People also ask

Does git commit record time?

git does not log when things are pushed into a repository (or fetched for that matter), only the two timestamps on the commits themselves, so I don't think there's a reliable way to do this without writing hooks that would store extra metadata somewhere for you, or relying on logging done by git-daemon, ssh, or your ...

How do you check if a commit was pushed?

you can use git log --graph --all --decorate , it will show where each ref is located (HEAD, master, origin/master etc.) This is my favorite answer because it gives the information EXPLICITLY: "commit XYZ (origin/master)".


2 Answers

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.

like image 193
MarcH Avatar answered Sep 24 '22 06:09

MarcH


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.

like image 20
Richard Hansen Avatar answered Sep 20 '22 06:09

Richard Hansen