当我推送到GitHub时可以隐藏提交时间吗?

问题描述:

正如标题所述:我希望我推送到GitHub的所有提交都在GitHub的提交标签中以推送的时间戳而不是提交的时间戳出现.

As the title says: I'd like all commits I push to GitHub to appear with the timestamp of the push rather than of the commit, in the GitHub's commits tab.

我使用非常标准的工作流程:

I use quite a standard workflow:

<do some work>
git add -u
git commit -m "Message 1."
...
<do some work>
git add -u
git commit -m "Message N."
git push myrepo master

这使得所有N个提交都显示在GitHub中,这很好,也很好.但是也显示了提交的时间,我不喜欢.我更希望只显示其中最后一个(或推送)的时间戳.

This makes all N commits show in GitHub, which is fine and good. But the time of the commits is shown as well, which I don't like. I would much more prefer to show only the timestamp of the last of them (or of the push).

GitHub推送时间

您可以在GitHub API上永久查看推送的时间.如果要隐藏它,别无选择:使用cron作业.

The time at which you push is forever viewable on the GitHub API. If you want to hide that, there is no alternative: use a cron job.

提交数据时间

可以通过环境变量控制提交数据的提交时间:

The commit time on the commit data can be controlled with the environment variables:

GIT_COMMITTER_DATE=2000-01-01T00:00:00+0000 \
GIT_AUTHOR_DATE=2000-01-01T00:00:00+0000 \
git commit -m 'my message'

对于--amend,在作者日期中使用--date选项:

For --amend, there use the --date option for the author date: How can one change the timestamp of an old commit in Git?

没有更多时间提交消息对象上的指示,这样就足够隐私了.

There are no further time indications on the commit message object, so that is enough for privacy.

您可能希望通过post-commit挂钩自动执行此操作,如以下说明所述:

You might want to do this automatically from post-commit hook as explained at: Can GIT_COMMITTER_DATE be customized inside a git hook?

这是一个更高级的挂钩,可让您的提交每天从午夜开始,并且

Here is a more advanced hook that makes your commits will start at midnight every day, and increment by one second for every new commit. This keeps commits sorted by time, while still hiding the commit time.

.git/hooks/post-commit

.git/hooks/post-commit

#!/usr/bin/env bash
if [ -z "${GIT_COMMITTER_DATE:-}" ]; then
  last_git_time="$(git log --date=format:'%H:%M:%S' --format='%ad' -n 1 --skip 1)"
  last_git_date="$(git log --date=format:'%Y-%m-%d' --format='%ad' -n 1 --skip 1)"
  today="$(date '+%Y-%m-%d')"
  if [ "$last_git_date" = "$today" ]; then
    new_time="${last_git_time}"
    new_delta=' + 1 second'
  else
    new_time="00:00:00"
    new_delta=
  fi
  d="$(date --date "${today}T${new_time}${new_delta}" "+${today}T%H:%M:%S+0000")"
  echo "$d"
  GIT_COMMITTER_DATE="$d" git commit --amend --date "$d" --no-edit
fi

GitHub上游.

别忘了:

chmod +x .git/hooks/post-commit

在git 2.19,Ubuntu 18.04上进行了测试.

Tested on git 2.19, Ubuntu 18.04.

别忘了也要处理:

  • git rebase with a post-rewrite hook: git rebase without changing commit timestamps
  • git am with --committer-date-is-author-date as explained at Can GIT_COMMITTER_DATE be customized inside a git hook?

否则提交者日期仍会泄漏.

or else the committer date still leaks.

批量历史记录修改

这是一种将现有范围内所有提交的提交时间固定为午夜,同时保持日期不变的方法:

Here is a way to fix the commit times for all commits in an existing range to midnight while keeping the date unchanged:

git-hide-time() (
  first_commit="$1"
  last_commit="${2:-HEAD}"
  git filter-branch --env-filter '
d="$(echo "$GIT_COMMITTER_DATE" | sed "s/T.*//")T00:00:00+0000)"
export GIT_COMMITTER_DATE="$d"
export GIT_AUTHOR_DATE="$d"
' --force "${first_commit}~..${last_commit}"
)

另请参阅:如何可以更改Git中旧提交的时间戳吗?