如果上次提交 msg 错误/有未提交的文件应该同上一次一起提交,需要重新提交备注:git commit --amend -m 'new msg'

修改上次提交的 author、email可以使用 :git commit --amend --author="newName <newEmail>"

修改整个历史记录中的某些错误的 author、email有两种方式 git rebase 或者 git filter-branch

# git rebase  模式
git rebase -i -p 76892625a7b126f4772f8d7e331ada3552c11ce1 
# 弹出编辑器,在需要修改的 commit 处 由 picked 改变为 edit ,然后 wq 退出 vim;
git commit --amend --author 'newName <newEmail>'
# 执行后即变更了相应的 author 和 email 
git rebase --continue 

git rebase 模式需要理解 git 的操作原理,步骤也比较多,可以直接使用 git filter-branch快速方便,直接复制下面脚本执行即可;git filter-branch 模式参考原理:https://help.github.com/articles/changing-author-info/

#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_COMMITTER_NAME="$CORRECT_NAME"
    export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
    export GIT_AUTHOR_NAME="$CORRECT_NAME"
    export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

效果如下图,我们发现历史记录中的author和email已经修改过来了;

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐