git修改提交作者和邮箱

作用一名程序员,我们会经常混迹与不同的代码仓库,时常不同仓库会有作者信息验证。比如公司内建的gitlab一般会要求统一使用公司内部的域账号签名;github要求使用github账号签名等。因此,很容易犯在不同库中提交代码发现默认配置的author信息没有变更,结果push被拒绝。

下面介绍几种常用的解决方式,当然最终还是需要养成切换代码库检查author信息的习惯,主动配置

// 设置全局
git config --global user.name "Author Name"
git config --global user.email "Author Email"

// 或者设置本地项目库配置
git config user.name "Author Name"
git config user.email "Author Email"

解决方法一

如果只需要最近一次提交,那么很简单直接使用git commit –amend就可以搞定

git commit --amend --author="NewAuthor <NewEmail@address.com>"

解决方法二

如果是多个修改,那么就需要使用到git filter-branch这个工具来做批量修改
为了方便大家使用,封装了一个简单的shell脚本,直接修改[XXX]中的变量为对应的值即可

#!/bin/sh

git filter-branch --env-filter '

an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "[Your Old Email]" ]
then
    cn="[Your New Author Name]"
    cm="[Your New Email]"
fi
if [ "$GIT_AUTHOR_EMAIL" = "[Your Old Email]" ]
then
    an="[Your New Author Name]"
    am="[Your New Email]"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'

猜你喜欢

转载自blog.csdn.net/diu_brother/article/details/51982993