serhii.net

In the middle of the desert you can say anything you want

15 Feb 2025

Git change name or email in commit history

This is brilliant: Git, rewrite previous commit usernames and emails - Stack Overflow

TL;DR

git config --global 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"

Then

git change-commits GIT_AUTHOR_NAME "old name" "new name"

# last 10 commits
git change-commits GIT_AUTHOR_EMAIL "old@email.com" "new@email.com" HEAD~10..HEAD

Depending on why I need this, I may need also GIT_COMMITTER_[NAME/EMAIL]

For multiple times, I created an change-commit-f that forces overwiriting the backup:

git config --global alias.change-commits-f '!'"f() { VAR=\$1; OLD=\$2; NEW=\$3; shift 3; git filter-branch -f --env-filter \"if  \\\"\$\`echo \$VAR\`\\\" = '\$OLD' ; then export \$VAR='\$NEW'; fi\" \$@; }; f"

A quick tldr from other answer, may be better but untested:

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 change-commits GIT_AUTHOR_NAME "<Old Name>" "<New Name>" -f
git change-commits GIT_AUTHOR_EMAIL <old@email.com> <new@email.com> -f
git change-commits GIT_COMMITTER_NAME "<Old Name>" "<New Name>" -f
git change-commits GIT_COMMITTER_EMAIL <old@email.com> <new@email.com> -f

(Previously: 220408-1822 Gitlab ‘you cannot push commits for ..’ error)

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus