<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ignytis</title>
    <description>The latest articles on DEV Community by Ignytis (@ignytis).</description>
    <link>https://dev.to/ignytis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1209963%2F0d144ed1-e066-4c38-beb7-9fdf2d2a1bfa.png</url>
      <title>DEV Community: Ignytis</title>
      <link>https://dev.to/ignytis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ignytis"/>
    <language>en</language>
    <item>
      <title>Rewriting the Git history: user name and e-mail</title>
      <dc:creator>Ignytis</dc:creator>
      <pubDate>Mon, 30 Dec 2024 14:55:44 +0000</pubDate>
      <link>https://dev.to/ignytis/rewriting-the-git-history-user-name-and-e-mail-5fi0</link>
      <guid>https://dev.to/ignytis/rewriting-the-git-history-user-name-and-e-mail-5fi0</guid>
      <description>&lt;p&gt;&lt;em&gt;This article is reposted from my blog:&lt;/em&gt;  &lt;a href="https://blog.ignytis.eu/posts/2024/07/09-git-history-rewrite/" rel="noopener noreferrer"&gt;https://blog.ignytis.eu/posts/2024/07/09-git-history-rewrite/&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  About
&lt;/h1&gt;

&lt;p&gt;This script will help you to delete your name and/or e-mail address from git history. It might be useful, for instance, if you had discovered a typo in your name or e-mail address, or just want to delete your real personal data.&lt;/p&gt;

&lt;h1&gt;
  
  
  The scope
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;User’s name and email to be changed&lt;/li&gt;
&lt;li&gt;A list of emails to patch will be provided. Only those comments which author’s email is listed will be rewritten. Other e-mails and names will stay unchanged&lt;/li&gt;
&lt;li&gt;Git history will be updated:

&lt;ul&gt;
&lt;li&gt;All branches&lt;/li&gt;
&lt;li&gt;All tags&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  Warning
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Note 1
&lt;/h2&gt;

&lt;p&gt;This method rewrites the Git history, so your modified local repository will be incompatible with original one (i.e. you will be unable to merge / push / pull from that repo). After rewriting the history you will need to force push into your original repo which is potentially dangerous. I suggest to have a backup of your repository to proceed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Note 2
&lt;/h2&gt;

&lt;p&gt;Obviously, this script will NOT help you in situation when somebody had forked your repo or any online tool had scraped your info from your public repo. In these cases you will need to contact with whoever is maintainer of those services or repository to erase your data&lt;/p&gt;

&lt;h1&gt;
  
  
  Preparation
&lt;/h1&gt;

&lt;p&gt;You need to fetch all branches and tags from your remote repo. An example script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Clone your project&lt;/span&gt;
git clone somegituser@gitexample.com:your_user/your_project.git
&lt;span class="c"&gt;# Go into Git directory&lt;/span&gt;
&lt;span class="nb"&gt;cd &lt;/span&gt;your_project

&lt;span class="c"&gt;# Maybe some extra here. Just to be 100% sure&lt;/span&gt;
git fetch &lt;span class="nt"&gt;--all&lt;/span&gt;
git pull &lt;span class="nt"&gt;--all&lt;/span&gt;
git fetch &lt;span class="nt"&gt;--tags&lt;/span&gt;
git pull &lt;span class="nt"&gt;--tags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  The script
&lt;/h1&gt;

&lt;p&gt;You can save this code to some file like &lt;code&gt;rewrite_history.sh&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; please review this code and define &lt;code&gt;CORRECT_NAME&lt;/code&gt;, &lt;code&gt;CORRECT_EMAIL&lt;/code&gt;, and list of emails to look up in for loop. It can be a single email too&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="nb"&gt;set&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="c"&gt;# exit on error&lt;/span&gt;

git filter-branch &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="nt"&gt;--env-filter&lt;/span&gt; &lt;span class="s1"&gt;'
CORRECT_NAME="Your desired new name"
CORRECT_EMAIL="your.desired.new.email@example.com"
for OLD_EMAIL in old.email1@example.com old.email2@example.com
do
  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
done
'&lt;/span&gt; &lt;span class="nt"&gt;--tag-name-filter&lt;/span&gt; &lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;--branches&lt;/span&gt; &lt;span class="nt"&gt;--tags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Patching the repository
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Step 1.&lt;/strong&gt; Run the script above e.g. &lt;code&gt;bash rewrite_history.sh&lt;/code&gt; from Git directory&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2.&lt;/strong&gt; Verification. Check git log if the result looks good to you. You might want to switch to other branches or tags to check them as well&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3.&lt;/strong&gt; Force push&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push &lt;span class="nt"&gt;--force&lt;/span&gt; origin your_branch:your_branch
&lt;span class="c"&gt;# ^ you need to do it with each branch. Check 'git branch' to see a full list of your branches&lt;/span&gt;
git push &lt;span class="nt"&gt;--force&lt;/span&gt; &lt;span class="nt"&gt;--tags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>git</category>
    </item>
  </channel>
</rss>
