问题已经说得很清楚了。
我希望通过使用自己的别名来混淆提交中用户真实电子邮件地址,从而为我的用户提供一些隐私保护。是否有一个钩子可以帮助我做到这一点?
您可以让他们在本地版本库中更改 user.email
属性即可。
git config user.email email@server.com
git commit --amend --author "$user <$email>" -C HEAD
,在每个提交进入您的副本时,可以保证您用一个新的、稍微不同的未发布提交替换一个未发布提交。(我假设您已经将此放在后提交钩子中。)[ -n "$richo_git_rewrite" ] && exit 0
吗?这是一种合理聪明的检测递归的方法。另一种方法是跳过递归检测,而是将提交中现有的用户和电子邮件与所需的用户和电子邮件进行比较。#! /bin/sh
# first, pick which git config variables to get
if ${SWITCHY-false}; then
config=user.work
else
config=user
fi
# next, find out if they're set
can_rewrite=false
target_author=$(git config --get $config.name) &&
target_email=$(git config --get $config.email) &&
can_rewrite=true
# If they ARE set, we can "rewrite" (replace) the commit;
# if not, we can't. Just because we can, though, does not
# mean we should. Find out if the current author and email
# differ from the desired ones.
if $can_rewrite; then
current_author=$(git log --pretty=format:%an HEAD -n 1)
current_email=$(git log --pretty=format:%ae HEAD -n 1)
if [ "$current_author" != "$target_author" -o \
"$current_email" != "$target_email" ]; then
# may want --allow-empty here, if you're allowing empty commits
# at all, otherwise empty ones don't get the rewrite done
git commit --amend --author "$target_author <$target_email>" -C HEAD
fi
fi
#! /bin/sh
fatal()
{
echo "$@" 1>&2
exit 1
}
# pick which git config variables to get
if ${SWITCHY-false}; then
config=user.work
else
config=user
fi
# tn, te = target author name/email
# an, ae = what git will use for author name/email
tn=$(git config --get $config.name) || exit 0
te=$(git config --get $config.email) || exit 0
an=${GIT_AUTHOR_NAME-$(git config --get user.name)} ||
fatal "no author name set"
ae=${GIT_AUTHOR_EMAIL-$(git config --get user.email)} ||
fatal "no author email set"
[ "$an" = "$tn" -a "$ae" = "$te" ] ||
fatal "git will use author $an <$ae>
but you want them as $tn <$te>
fix your environment variables and try again"
$GIT_AUTHOR_NAME
和$GIT_AUTHOR_EMAIL
,如果它们没有设置,则使用git config get,然后将它们(类似于我的示例post-commit hook)与"desired"值进行比较。如果它们不匹配,则退出1,停止提交。(当然,将其放在几乎所有的pre-everything上。) - torekhttp://progit.org/book/ch6-4.html 解释了如何重写历史记录。在底部附近,有这个命令
git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ];
then
GIT_AUTHOR_NAME="Scott Chacon";
GIT_AUTHOR_EMAIL="schacon@example.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
这个命令会将所有提交者从 schacon@localhost
改为 schacon@example.com
。
注意:这将改变所有从最早的电子邮件更改开始的提交 ID。