如何更改一系列提交的作者?


当前回答

这不是对你问题的回答,而是一个脚本,你可以用它来避免将来发生这种情况。它利用Git 2.9版以来可用的全局钩子,根据您所在的目录检查您的电子邮件配置:

#!/bin/sh
PWD=`pwd`
if [[ $PWD == *"Ippon"* ]] # 1)
then
  EMAIL=$(git config user.email)
  if [[ $EMAIL == *"Work"* ]] # 2)
  then
    echo "";
  else
    echo "Email not configured to your Work email in the Work directory.";
    git config user.email "youremail@youremail.com"
    echo "Git email configuration has now been changed to \"$(git config user$
    echo "\nPlease run your command again..."
    echo ''
    exit 1
  fi;
elif [[ $PWD == *"Personal"* ]]
then
  EMAIL=$(git config user.email)
  if [[ $EMAIL == "youremail@youremail.com" ]]
  then
    echo "";
  else
    echo "Email is not configured to your personal account in the Personal di$
    git config user.email "youremail@youremail.com"
    echo "Git email configuration has now been changed to \"$(git config user$
    echo "\nPlease run your command again..."
    echo ''
    exit 1;
  fi;
fi; 

它检查您当前的工作目录,然后验证您的git是否配置为正确的电子邮件。如果没有,它会自动更改。请在此处查看详细信息。

其他回答

对于重置当前用户和当前时间戳的所有提交(包括首次提交):

git rebase --root --exec "git commit --amend --no-edit --date 'now' --reset-author"

我发现所提供的版本非常激进,特别是如果你从其他开发人员那里提交补丁,这将从本质上窃取他们的代码。

下面的版本确实适用于所有分支机构,并分别更改作者和发件人以防止这种情况发生。

感谢leif81的所有选择。

#!/bin/bash

git filter-branch --env-filter '
if [ "$GIT_AUTHOR_NAME" = "<old author>" ];
then
    GIT_AUTHOR_NAME="<new author>";
    GIT_AUTHOR_EMAIL="<youmail@somehost.ext>";
fi
if [ "$GIT_COMMITTER_NAME" = "<old committer>" ];
then
    GIT_COMMITTER_NAME="<new commiter>";
    GIT_COMMITTER_EMAIL="<youmail@somehost.ext>";
fi
' -- --all

注意,git存储了两个不同的电子邮件地址,一个是提交人(提交更改的人),另一个是作者(编写更改的人员)。

提交者信息在大多数地方都不会显示,但您可以通过git-log-1--format=%cn,%ce(或使用show而不是log来指定特定的提交)看到它。

虽然更改上一次提交的作者与gitcommit一样简单--修改--author“作者名称”<email@example.com>“,没有一个liner或argument对提交者信息做同样的处理。

解决方案是(暂时或不)更改您的用户信息,然后修改提交,这将更新提交者到您的当前信息:

git config user.email my_other_email@example.com 
git commit --amend

当您没有初始化$HOME/.gitconfig时,就会发生这种情况。您可以将其修复为:

git config --global user.name "you name"
git config --global user.email you@domain.example
git commit --amend --reset-author

使用Git 1.7.5.4版进行测试。

注意,这只修复了最后一次提交。

如果您想(轻松)更改当前分支的作者,我会使用类似的方法:

# update author for everything since origin/master
git rebase \
  -i origin/master \
  --exec 'git commit --amend --no-edit --author="Author Name <author.name@email.co.uk>"'