cmc/cleberg.net
My personal web garden & blog.
clone: git clone https://gitbay.org/cmc/cleberg.net.git
main: content/blog/2021-05-30-changing-git-authors.org · raw
1#+date: [2021-05-30 Sun 00:00:00]
2#+title: Correcting Git Author Details Across Multiple Repos
3#+description: A script to update the git author name and email across multiple repos.
4#+slug: changing-git-authors
5#+filetags: :personal:
6
7* Changing Git Author/Email Based on Previously Committed Email
8
9Here's the dilemma: You've been committing changes to your Git repository with
10an incorrect name or email (or multiple repositories), and now you want to fix
11it. Luckily, there's a semi-reliable way to fix that. While I have never
12experienced issues with this method, some people have warned that it can mess
13with historical hashes and integrity of commits, so use this method only if
14you're okay accepting that risk.
15
16Okay, let's create the bash script:
17
18#+begin_src sh
19nano change_git_authors.sh
20#+end_src
21
22The following information can be pasted directly into your bash script. The only
23changes you need to make are to the following variables:
24
25- =OLD_EMAIL=
26- =CORRECT_NAME=
27- =CORRECT_EMAIL=
28
29#+begin_src sh
30#!/bin/sh
31
32# List all sub-directories in the current directory
33for dir in */
34do
35 # Remove the trailing "/"
36 dir=${dir%*/}
37 # Enter sub-directory
38 cd $dir
39
40 git filter-branch --env-filter '
41
42 OLD_EMAIL="old@example.com"
43 CORRECT_NAME="your-new-name"
44 CORRECT_EMAIL="new@example.com"
45
46 if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
47 then
48 export GIT_COMMITTER_NAME="$CORRECT_NAME"
49 export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
50 fi
51 if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
52 then
53 export GIT_AUTHOR_NAME="$CORRECT_NAME"
54 export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
55 fi
56 ' --tag-name-filter cat -- --branches --tags
57
58 git push --force --tags origin 'refs/heads/*'
59
60 cd ..
61done
62#+end_src
63
64Finally, save the bash script and make it executable.
65
66#+begin_src sh
67chmod a+x change_git_authors.sh
68#+end_src
69
70Now you can run the script and should see the process begin.
71
72#+begin_src sh
73./change_git_authors.sh
74#+end_src