yo'*_*yo' 5 git logging timestamp github
正如标题所说:我希望我推送到 GitHub 的所有提交都带有推送的时间戳,而不是提交的时间戳,显示在 GitHub 的提交选项卡中。
我使用相当标准的工作流程:
<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 中,这很好。但是也显示了提交的时间,我不喜欢。我更喜欢只显示最后一个(或推送)的时间戳。
GitHub 将推送时间永久存储在其数据库中
例如,您推送的时间可以在 GitHub Events API 上永久查看。
例如,https://api.github.com/repos/cirosantilli/china-dictatorship/events现在包含实际推送数据“2020-12-29T11:37:26Z”:
          "message": "66d9be0cb84c9650d6181b4b0fc2b70e2178bd9e",
          "distinct": true,
          "url": "https://api.github.com/repos/cirosantilli/china-dictatorship/commits/0817ad58873c5656d2828613a5a24ca8f286e910"
        }
      ]
    },
    "public": true,
    "created_at": "2020-12-29T11:37:26Z"
  },
即使实际提交是“Tue Dec 29 00:00:00 2020 +0000”。
这个API数据直接存储在他们的数据库中,而不是在Git提交数据中,并且没有办法伪造它。
如果你想隐藏这一点,据我所知,没有其他选择,你必须:
这是一个隐私问题,尽管坚定的攻击者当然能够通过轮询某些感兴趣的用户的个人资料来获得类似的结果。相关门票:
提交数据时间
提交数据的提交时间可以通过环境变量控制:
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作者日期选项:How can one change the timestamp of an old commit in Git?
提交消息对象上没有进一步的时间指示,因此这足以保护隐私。
您可能希望从钩子自动执行此操作post-commit,如以下所述:Can GIT_COMMITTER_DATE becustomized inside a git hook?
这是一个更高级的钩子,使您的提交将在每天午夜开始,并为每个新提交增加一秒。这使得提交按时间排序,同时仍然隐藏提交时间。
.git/hooks/提交后
#!/usr/bin/env bash
set -eu
echo post-rewrite
if [ ! "${CIROSANTILLI_GITHOOKS_DISABLE:-0}" = 1 ]; then
  declare -a olds
  declare -A oldnew
  while IFS= read -r line; do
    echo "$line"
    old="$(echo "$line" | cut -d ' ' -f1)"
    new="$(echo "$line" | cut -d ' ' -f2)"
    oldnew[$old]="$new"
    olds+=("$old")
    news+=("$new")
  done
  # Save unstaged changes. Otherwise e.g. git commit --amend destroys them!
  # /sf/ask/1716455401/#38785582
  nstash="$(git stash list | wc -l)"
  git stash
  git reset --hard "${news[0]}~"
  for old in "${olds[@]}"; do
    new="${oldnew[$old]}"
    git cherry-pick "$new" &>/dev/null
    olddate="$(git log --format='%cd' -n 1 "$old")"
    CIROSANTILLI_GITHOOKS_DISABLE=1 \
      GIT_COMMITTER_DATE="$olddate" \
      git commit \
      --amend \
      --no-edit \
      --no-verify \
      &>/dev/null \
    ;
  done
  # Restore unstaged changes.
  if [ "$(git stash list | wc -l)" -ne "$nstash" ]; then
    git stash apply
  fi
  echo
fi
不要忘记:
chmod +x .git/hooks/post-commit
在 git 2.19、Ubuntu 18.04 上测试。
不要忘记还处理:
git rebase带post-rewrite钩子:git rebase 而不更改提交时间戳git am如Can GIT_COMMITTER_DATE can becustomized inside a git hook? 中--committer-date-is-author-date所述否则提交者日期仍然会泄露。
批量历史修改
以下是一种将现有范围内所有提交的提交时间固定为午夜的方法,同时保持日期不变:
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 中旧提交的时间戳?
| 归档时间: | 
 | 
| 查看次数: | 3387 次 | 
| 最近记录: |