content/blog/2021-05-30-changing-git-authors.org

e8577d98faff1bed72ba6f2d204b8b44c6827c23
cleberg.net/content/blog/2021-05-30-changing-git-authors.org rendered · source · history · blame · raw

74 lines · 2155 bytes

 1#+date:        <2021-05-30 Sun 00:00:00>
 2#+title:       Procedure for Modifying Git Committer Author Name and Email Across Multiple Repositories
 3#+description: Detailed instructions for systematically updating the Git author name and email information in multiple repositories using command-line tools and scripts.
 4#+slug:        changing-git-authors
 5#+filetags:    :git:automation:scripting:
 6
 7* Changing Git Author/Email Based on Previously Committed Email
 8
 9Here's the dilemma: You've been committing changes to your git
10repository with an incorrect name or email (or multiple repositories),
11and now you want to fix it. Luckily, there's a semi-reliable way to fix
12that. While I have never experienced issues with this method, some
13people have warned that it can mess with historical hashes and integrity
14of commits, so use this method only if you'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.
23The only changes 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