Can I hide commits' time when I push to GitHub

2019-05-09 13:00发布

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

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

2条回答
Melony?
2楼-- · 2019-05-09 13:46

GitHub push time

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.

Commit data time

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'

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.

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

#!/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 upstream.

Don't forget to:

chmod +x .git/hooks/post-commit

Tested on git 2.19, Ubuntu 18.04.

Don't forget to also handle:

or else the committer date still leaks.

Bulk history modification

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}"
)

See also: How can one change the timestamp of an old commit in Git?

查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-09 13:58

You have several options:

  • Write your webhooks, catch the push event and modify the date to the current timestamp.
  • A better approch is to write local hook that on the pre-push modify your timestamp
查看更多
登录 后发表回答