Now that you have corrected commit in object database, you have to tell git to automatically and transparently replace wrong commit by corrected one using git replace command:
$ git replace <ID of wrong commit> <ID of corrected commit>
Finally, list all replacement to check if this procedure succeded
$ git replace -l
并检查是否有更换
$ git log --author=user@wrong.email --all
You can of course automate this procedure... well, all except using git replace which doesn't have (yet) batch mode, so you would have to use shell loop for that, or replace "by hand".
NOT TESTED! YMMV.
Note that you might encounter some rough corners when using refs/replace/ mechanism: it is new, and not yet very well tested.
在只有前几个提交有坏作者的情况下,您可以使用exec命令和--amend提交在git rebase -i中执行此操作,如下所示:
git rebase -i HEAD~6 # as required
它为您提供可编辑的提交列表:
pick abcd Someone else's commitpick defg my bad commit 1pick 1234 my bad commit 2
然后在所有作者不好的行之后添加exec ... --author="..."行:
pick abcd Someone else's commitpick defg my bad commit 1exec git commit --amend --author="New Author Name <email@address.example>" -C HEADpick 1234 my bad commit 2exec git commit --amend --author="New Author Name <email@address.example>" -C HEAD
#!/bin/sh
REPO_URL=ssh://path/to/your.gitREPO_DIR=rewrite.tmp
# Clone the repositorygit clone ${REPO_URL} ${REPO_DIR}
# Change to the cloned repositorycd ${REPO_DIR}
# Checkout all the remote branches as local tracking branchesgit branch --list -r origin/* | cut -c10- | xargs -n1 git checkout
# Rewrite the history, use a system that will preseve the eol (or lack of in commit messages) - preferably Linux not OSXgit filter-branch --env-filter 'OLD_EMAIL="me@something.com"CORRECT_NAME="New Me"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]thenexport GIT_COMMITTER_NAME="$CORRECT_NAME"fiif [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]thenexport GIT_AUTHOR_NAME="$CORRECT_NAME"fi' --tag-name-filter cat -- --branches --tags
# Force push the rewritten branches + tags to the remotegit push -f
# Remove all knowledge that we did somethingrm -rf ${REPO_DIR}
# Tell your colleagues to `git pull --rebase` on all their local remote tracking branches
git rebase -i YOUR_FIRTS_COMMIT_SHA^
while true; do git commit --amend --author="Name Surname <email@example.com>" --no-edit && git rebase --continue; done
将提交author name & email更改为Amend,然后替换old-commit with new-one:
$ git checkout <commit-hash> # checkout to the commit need to modify$ git commit --amend --author "name <author@email.com>" # change the author name and email
$ git replace <old-commit-hash> <new-commit-hash> # replace the old commit by new one$ git filter-branch -- --all # rewrite all futures commits based on the replacement
$ git replace -d <old-commit-hash> # remove the replacement for cleanliness$ git push -f origin HEAD # force push
Another way Rebasing:
$ git rebase -i <good-commit-hash> # back to last good commit
# Editor would open, replace 'pick' with 'edit' before the commit want to change author
$ git commit --amend --author="author name <author@email.com>" # change the author name & email
# Save changes and exit the editor
$ git rebase --continue # finish the rebase
#!/bin/shPWD=`pwd`if [[ $PWD == *"Ippon"* ]] # 1)thenEMAIL=$(git config user.email)if [[ $EMAIL == *"Work"* ]] # 2)thenecho "";elseecho "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 1fi;elif [[ $PWD == *"Personal"* ]]thenEMAIL=$(git config user.email)if [[ $EMAIL == "youremail@youremail.com" ]]thenecho "";elseecho "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;
# update author for everything since origin/mastergit rebase \-i origin/master \--exec 'git commit --amend --no-edit --author="Author Name <author.name@email.co.uk>"'