可以在git hook中自定义GIT_COMMITTER_DATE吗? [英] Can GIT_COMMITTER_DATE be customized inside a git hook?

查看:191
本文介绍了可以在git hook中自定义GIT_COMMITTER_DATE吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想手动控制git commit时间戳,以便我的GIT_COMMITTER_DATE始终与GIT_AUTHOR_DATE匹配.我已经看到许多使用filter-branch重写历史记录的解决方案,但是我宁愿对此保持主动,并将逻辑放在git hook中,以便它始终与向前匹配.

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.

但是我发现,虽然这些变量如果在调用git的环境中定义时可以很好地工作,但是当在pre-commit git钩子中定义它们时,它们似乎没有任何作用.例如:

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"

有什么方法可以在git钩子中动态调整这些值,从而使提交自动具有所需的时间戳?我使用的是git版本1.8.5.2

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

推荐答案

post-commit挂钩+ git commit --amend

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

.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

别忘了:

chmod +x .git/hooks/post-commit

我们检查GIT_COMMITTER_DATE以防止其进入无限提交循环,如果用户已经过去了特定时间,它也会跳过该钩子.

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.

这是一个更复杂的示例,该示例使用通过git logdate操纵的先前提交的日期:

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?

请记住,提交者日期仍然在git rebase上泄漏,但是可以通过post-rewrite钩子解决:

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

然后还有git am,可以使用--committer-date-is-author-date进行解决,如下所述:

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

在以下位置询问了--amend --date部分:更新git commit作者修改日期

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.

在Git 2.19上进行了测试.

Tested on Git 2.19.

这篇关于可以在git hook中自定义GIT_COMMITTER_DATE吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆