Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Fix old git commit author info

To learn which repos need the info corrected:

cd ~/dev/sync-git/
for word in * ; do ( cd "${word}" ; git log --pretty=format:'%an,%ae' | sort -u | sed -r -e "s/^/${word},/;" ; ) ; done | grep 'localhost|example' ~/fix3

I inspected ~/fix3 and found two projects that had incorrect email addresses.

Now, prepare a script, ~/fix-git-commits.sh

#!/bin/sh
# Startdate: 2023-01-30-2 14:00
# ripped from https://www.git-tower.com/learn/git/faq/change-author-name-email/
git filter-branch --env-filter '
WRONG_EMAILS="localhost"
NEW_NAME="B. Stack"
NEW_EMAIL="bgstack15@gmail.com"
if echo "${GIT_COMMITTER_EMAIL}" | grep -qE "${WRONG_EMAILS}" ;
then
    export GIT_COMMITTER_NAME="${NEW_NAME}"
    export GIT_COMMITTER_EMAIL="${NEW_EMAIL}"
fi
if echo "${GIT_AUTHOR_EMAIL}" | grep -qE "${WRONG_EMAILS}" ;
then
    export GIT_AUTHOR_NAME="${NEW_NAME}"
    export GIT_AUTHOR_EMAIL="${NEW_EMAIL}"
fi
' --tag-name-filter cat -- --branches --tags

In the two repositories that needed to have info corrected, run this script.

cd ~/dev/project1
sh ~/fix-git-commits.sh

Then, you can just force-push each branch to each remote.

I also had to manually re-tag each tag. I just matched exact commit time and manually ran:

git tag --force v2.1 330b3e91f357f54668c54d5e543a7f3138b77ad7

And then I force-pushed the tags to each remote.

git push --tags local --force
git push --tags cloud --force

Future reading

  1. Gitlab has some extra steps I should bother to do, but I haven't yet: https://docs.gitlab.com/ee/user/project/repository/reducing_the_repo_size_using_git.html

References

  1. Slightly modified from https://www.git-tower.com/learn/git/faq/change-author-name-email/

Comments