7

I'd like to manually control the git commit timestamp so that my GIT_COMMITTER_DATE always matches the GIT_AUTHOR_DATE. I've seen many solutions using filter-branch to rewrite history, but I'd rather be proactive about this and put the logic in a git hook so that it always matches going forward.

But I find that while these variables work fine if defined in the environment where git is invoked, they do not seem to have any effect when they are defined inside the pre-commit git hook. Eg:

# this works if run directly on cmd line, but not inside the pre-commit hook
export GIT_AUTHOR_DATE='Mon, 11 Aug 2014 11:25:16 -0400'
export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"

Is there any way to dynamically adjust these values inside a git hook so that commits automatically have the desired timestamps? I'm on git version 1.8.5.2

Magnus
  • 10,736
  • 5
  • 44
  • 57
  • You are aware that according to [man page](http://manpages.ubuntu.com/manpages/gutsy/man1/cg-commit.1.html), GIT_COMMITTER_DATE "should be never overridden, unless you know you absolutely need to override it (to ensure the commit gets the same ID as another or when migrating history around)"? – eis Sep 21 '15 at 16:10
  • These variables are provided for the express purpose of manipulation, which is routinely done on the command line. What difference does it make if I now want to do that same manipulation in a git hook? – Magnus Sep 21 '15 at 17:27
  • that's not really true, as command line affects author date only, not committer date. You can't change committer date from the command line variables, only through environment variables. But no, I don't know the root cause why the man page says so. – eis Sep 21 '15 at 19:17
  • I've been using aliases as a ugly workaround: https://stackoverflow.com/questions/28832219/can-i-hide-commits-time-when-i-push-to-github It's sad. – Ciro Santilli OurBigBook.com Nov 18 '18 at 23:51

1 Answers1

5

post-commit hook + git commit --amend

This is not super elegant, but it seems to work and sets both committer and author date:

.git/hooks/post-commit

#!/usr/bin/env bash
if [ -z "${GIT_COMMITTER_DATE:-}" ]; then
  d="$(date --iso-8601=seconds)"
  GIT_COMMITTER_DATE="$d" git commit --amend --date "$d" --no-edit
fi

Don't forget to:

chmod +x .git/hooks/post-commit

We check for GIT_COMMITTER_DATE to prevent it from entering into an infinite commit loop, and it also skips the hook if the user is already passing a specific time.

Here is a more sophisticated example that uses the date from previous commits through git log and date manipulations: Can I hide commits' time when I push to GitHub?

Remember that the committer date still leaks on git rebase, but that can be solved with a post-rewrite hook: git rebase without changing commit timestamps

Then there is also git am, which can be solved with --committer-date-is-author-date as mentioned at: git rebase without changing commit timestamps

The --amend --date part was asked at: Update git commit author date when amending

You could also set that to a global hook: Applying a git post-commit hook to all current and future repos but core.hooksPath prevents local hooks from running at all which might be a problem.

Tested on Git 2.19.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985