Skip to content

Instantly share code, notes, and snippets.

@bitIO
Last active August 13, 2022 11:41
Show Gist options
  • Save bitIO/a2435fbabb439cd2868c1275fa2e5f0e to your computer and use it in GitHub Desktop.
Save bitIO/a2435fbabb439cd2868c1275fa2e5f0e to your computer and use it in GitHub Desktop.
How to rewrite history in a git repo
#!/bin/sh
## change commits
#######################################################
git filter-repo --commit-callback '
msg = commit.message.decode(\"utf-8\")
newmsg = msg.replace(\"old string\", \"new string\")
commit.message = newmsg.encode(\"utf-8\")
' --force
## change commiter - filter-repo
#######################################################
cd repo
git filter-repo --mailmap my-mailmap
#my-mailmap file content
Correct Name <correct@email.com> <old@email.com>
## change commiter - filter-branch
#######################################################
git filter-branch --env-filter '
OLD_EMAIL="you@wrong.email"
CORRECT_NAME="Your name"
CORRECT_EMAIL="You@correct.email"
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 -f
git push --force --tags origin 'refs/heads/*'
@bitIO
Copy link
Author

bitIO commented Nov 5, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment