【技巧】如何灵活修改 git 仓库的作者信息
修改默认提交时的作者信息如果首次使用 git,或者接手了一台新的电脑重新安装了git,提交时候需要我们添加默认作者信息,这属于长期修改,如果不再进行第二次操作,这个作者信息会被一直保存,并且在我们每一次提交时默认使用。修改默认的方法有两种,一种是全局修改,一种是仅修改当前仓库:全局修改:本机所有git仓库均被改变$ git config --global user.name "zhangsan"$
修改默认提交时的作者信息
如果首次使用 git,或者接手了一台新的电脑重新安装了git,提交时候需要我们添加默认作者信息,这属于长期修改,如果不再进行第二次操作,这个作者信息会被一直保存,并且在我们每一次提交时默认使用。修改默认的方法有两种,一种是全局修改,一种是仅修改当前仓库:
全局修改:本机所有git仓库均被改变
$ git config --global user.name "zhangsan"
$ git config --global user.email "zhangsan@123.com"
当前仓库:只修改当前仓库
$ git config user.name "zhangsan"
$ git config user.email "zhangsan@123.com"
临时修改下一次提交的作者信息
例如 lisi 在 zhangsan 的电脑上修复了一个 bug,如果直接提交,作者信息默认是 zhangsan,如果想在不修改 zhangsan 电脑 git 配置的情况下添加 lisi 的信息,就需要临时修改作者信息:
git 加上 author 选项:
git commit --author="lisi <lisi@123.com>"
修改已经提交的作者信息
仅修改上一次提交:
git commit --amend --author="wang wu <wangwu@123.com>"
修改之前的任意一次提交
- 交互式的
rebase
到边界 commit idgit rebase -i -p 9898dekh
- 为需要修改作者的历史 commit 添加
edit
前缀 - 为添加了
edit
前缀的提示提交追加作者信息$ git commit --amend --author="wang wu <wangwu@123.com>" --no-edit $ git rebase --continue
大批量修改已经提交的作者信息
编写 filter-branch
脚本
Use Git’s “filter-branch” command. It allows you to batch-process a (potentially large) number of commits with a script.
You can run the below sample script in your repository (filling in real values for the old and new email and name):
$ git filter-branch --env-filter '
WRONG_EMAIL="wrong@example.com"
NEW_NAME="New Name Value"
NEW_EMAIL="correct@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_COMMITTER_NAME="$NEW_NAME"
export GIT_COMMITTER_EMAIL="$NEW_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$WRONG_EMAIL" ]
then
export GIT_AUTHOR_NAME="$NEW_NAME"
export GIT_AUTHOR_EMAIL="$NEW_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
The same warning applies to this method as to the others mentioned: you are rewriting history with this command, creating new commit objects along the way!
Preferably, you should only do this in repositories that haven’t been published / shared, yet. In any other case you should use it with extreme care - and only if you’re aware of the side effects!
更多推荐
所有评论(0)