aboutsummaryrefslogtreecommitdiff
path: root/content/blog/2021-05-30-changing-git-authors.org
blob: 9fa5285900d66ca3a0c14d15fc8404d391426043 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#+date: <2021-05-30>
#+title: Changing Git Authors 
#+description: 
#+slug: changing-git-authors

* Changing Git Author/Email Based on Previously Committed Email

Here's the dilemma: You've been committing changes to your git
repository with an incorrect name or email (or multiple repositories),
and now you want to fix it. Luckily, there's a semi-reliable way to fix
that. While I have never experienced issues with this method, some
people have warned that it can mess with historical hashes and integrity
of commits, so use this method only if you're okay accepting that risk.

Okay, let's create the bash script:

#+begin_src sh
nano change_git_authors.sh
#+end_src

The following information can be pasted directly into your bash script.
The only changes you need to make are to the following variables:

- =OLD_EMAIL=
- =CORRECT_NAME=
- =CORRECT_EMAIL=

#+begin_src sh
#!/bin/sh

# List all sub-directories in the current directory
for dir in */
do
    # Remove the trailing "/"
    dir=${dir%*/}
    # Enter sub-directory
    cd $dir

    git filter-branch --env-filter '

    OLD_EMAIL="old@example.com"
    CORRECT_NAME="your-new-name"
    CORRECT_EMAIL="new@example.com"

    if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
    then
        export GIT_COMMITTER_NAME="$CORRECT_NAME"
        export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
    fi
    if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
    then
        export GIT_AUTHOR_NAME="$CORRECT_NAME"
        export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
    fi
    ' --tag-name-filter cat -- --branches --tags

    git push --force --tags origin 'refs/heads/*'

    cd ..
done
#+end_src

Finally, save the bash script and make it executable.

#+begin_src sh
chmod a+x change_git_authors.sh
#+end_src

Now you can run the script and should see the process begin.

#+begin_src sh
./change_git_authors.sh
#+end_src