Can GIT_COMMITTER_DATE be customized inside a git hook?

不想你离开。 提交于 2019-12-01 21:08:51

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 .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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!