Last active
December 16, 2020 11:29
-
-
Save maxpeterson/ffc196f739404066997b7b9d002114a3 to your computer and use it in GitHub Desktop.
Rewrite all commits to anonymise author and committer's name and email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Each author and committer's name and email will be replaced by an anonymous value | |
# Create a git alias | |
# git change-commits GIT_AUTHOR_NAME "old name" "new name" | |
git config alias.change-commits '!'"f() { VAR=\$1; OLD=\$2; NEW=\$3; shift 3; git filter-branch --env-filter \"if [[ \\\"\$\`echo \$VAR\`\\\" = '\$OLD' ]]; then export \$VAR='\$NEW'; fi\" \$@; }; f " | |
# Git valriables to replace with git log formats to generate a list of values toi change | |
declare -a formats=("GIT_AUTHOR_NAME::%aN" "GIT_AUTHOR_EMAIL::%aE" "GIT_COMMITTER_NAME::%cN" "GIT_COMMITTER_EMAIL::%cE"); | |
formatslength=${#formats[@]}; | |
filename="git-anonymise-$(date '+%Y-%m-%d-%H-%M-%S').csv"; | |
echo 'Branch\tName\tOld Value\tNew Value\tReverse' > $filename; | |
ls .git/refs/heads | while read -r branch ; do | |
echo "Processing branch $branch"; | |
for index in "${formats[@]}" ; do | |
gitName="${index%%::*}"; | |
format="${index##*::}"; | |
newValueFmt=$([[ $gitName == *_EMAIL ]] && echo 'echo [email protected]' || echo 'echo Anonymous $counter'); | |
echo "Processing $gitName"; | |
counter=0; | |
git log --all --format="$format" | sort -u | while read -r oldValue ; do | |
counter=$[counter + 1]; | |
newValue=$(eval $newValueFmt); | |
echo "$branch"'\t'"$gitName"'\t'"$oldValue"'\t'"$newValue"'\t'"git change-commits '$gitName' '$newValue' '$oldValue' '$branch'" >> $filename; | |
git change-commits "$gitName" "$oldValue" "$newValue" "$branch"; | |
# Delete the old commits | |
rm -rf .git/refs/original/ | |
done | |
done | |
done | |
echo "Anonymisation mapping in $filename" | |
# Delete remotes, which might point to the old commits | |
git remote | while read -r name ; do git remote rm $name; done | |
# Your old commits will now no longer show up in GitK, `git log` or `git | |
# reflog`, but can still be found using `git show $commit-id`. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
filter-repo
Create a mapping based on the git shortlog email summary
Ensue all committer are included (not just authors), using ...
Update mailmap-author.txt to include the mapping using any of the following formats:
Run the mapping (this will rewrite git history so ensure you have a backup of the repo)
The filter-repo command will remove the original remote …
Add the new remote.
Push to the remote
Also see https://tanimislamblog.wordpress.com/2019/11/05/how-to-use-git-filter-repo-to-change-inconvenient-emails/