Silly Git Mistakes! Fixing Author name in git history
Tags: git, github, developer, beginners
Published: 2023-10-05 18:51:03

Your Git commit history is not only a record of your project's progress but also a reflection of the individuals who contributed to its development. However, there are times when you may need to correct or update author information for multiple commits. Whether it's fixing a typo in your email address or ensuring your commits are properly attributed, Git provides powerful tools for reshaping your commit history. 😎

In this blog post, I will explore an essential Git task that every developer should know: changing the author information for all commits in your repository. We'll dive into the process step by step, covering both classic and modern methods, and provide tips on best practices to avoid common pitfalls. 😉

Join me on this journey to unlock the full potential of your Git history, as we unveil the secrets to seamlessly updating author details for all your commits. Let's dive in!

When you are working with git and using `Github` may of you may have encountered a condition when you noticed that you pushed a commit to master branch but still you commit in not showing in your contributions graph. That time every dev's reaction.....


![Frustrated Developer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hsib634q4w06x18yox97.gif)

Very common mistake which developer do is setting the wrong username and email in the `git config`
Now if you don't remember what's this then it's the very first step you do when you start working with git by setting the username and email in your git config. 😯

Now, This is very common when you are working with two different git accounts (work and personal) and set username and email globally using 

```
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
```

Before jumping into the solution let's know how to avoid this thing in future if you haven't faced this issue.

1. Avoid working with different git account in same PC.
2. Type you email and username correctly when setting `git config` value.
3. Avoid setting username and email globally and instead set it locally
```
$ git config --local user.name "John Doe"
$ git config --local user.email johndoe@example.com
```

**Now if already did the mistake then.........**

![How do we fix this](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6ozzeycv1qdcpkw4q4n8.gif)

**Relax............**

![Relax](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i15ht0vvwww53plmltr6.gif)


![I have solution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qn9x6w1cdx6m3qz3wgz3.gif)


**_But before proceeding to solution. _**
_Warning: Changing commit information for all commits in your repository will rewrite the entire commit history, and it can be very disruptive if you're working in a shared repository. Make sure you have a good reason to do this, and communicate with your team before proceeding.
Do at your own risk._


**_There are two way of doing this._**

1. Git Filter Branch (Less Risky)
2. Git Rebase (Risky sometimes)


> **_Git Filter Branch (Less Risky)_**

For future use as well and doing in multiple repos I recommend creating a script which you can use in any repo and save it somewhere easily.

- If you want to change all commits author 
```
#!/bin/sh

git filter-branch --env-filter '
  export GIT_AUTHOR_NAME="New Author Name";
  export GIT_AUTHOR_EMAIL="new.email@example.com";
  export GIT_COMMITTER_NAME="New Author Name";
  export GIT_COMMITTER_EMAIL="new.email@example.com";
' -- --all

```
- You want to change only specific ones
``` 
git filter-branch --env-filter '
  OLD_EMAIL="old@email.com"
  NEW_EMAIL="new@email.com"
  NEW_NAME="John Doe"

  if test "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL"
  then
    GIT_AUTHOR_EMAIL=$NEW_EMAIL
    GIT_AUTHOR_NAME=$NEW_NAME
  fi

  if test "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL"
  then
    GIT_COMMITTER_EMAIL=$NEW_EMAIL
    GIT_COMMITTER_NAME=$NEW_NAME
  fi
' -- --all
```
Make sure to change your name and email to the correct ones.

After that make the script executable.

```
chmod +x update_author_info.sh
```
Now just run the script

```
./update_author_info.sh

```
Force-push the rewritten branch to update the remote repository:

```
git push origin --all --force
```

> Git Rebase (Risky sometimes)

- The first step is to identify the commits that you want to modify. You can do this by using the git log command, which displays the commit history of your repository.

```
git log
```
It should show the information like this 

```
commit 5e3f7ba5d4249f5c06a4e5f4b3d1e7f7d6d1b917
Author: John Smith <john@example.com>
Date:   Tue Jun 1 12:34:56 2021 -0500

    Add new feature

commit 2a7b9a1e9f6d7b5c8a3a714d61f6d7e1e0b8a6c2
Author: Jane Doe <jane@example.com>
Date:   Mon May 31 11:22:33 2021 -0500

    Fix bug

commit 8c8e1b4d2b4a2d4f4a4e4f4d6b6c6f0d8c8e8b2d
Author: John Smith <john@example.com>
Date:   Sun May 30 10:11:12 2021 -0500

    Initial commit
```

- Start an Interactive Rebase

```
git rebase -i HEAD~3
```
This command tells Git to start an interactive rebase from the current HEAD (the latest commit) up to three commits back. You can adjust the number to include more or fewer commits, depending on your needs.

After running this command, Git will open a text editor with a list of all the commits that you want to modify. Each commit is listed with a hash code, the author name and email, the commit date, and the commit message.

```
pick 8c8e1b4 Initial commit
pick 2a7b9a1 Fix bug
pick 5e3f7ba Add new feature
```
- Now it’s time to edit the commits. For each commit that you want to modify, change the pick command to edit.

```
edit 8c8e1b4 Initial commit
pick 2a7b9a1 Fix bug
edit 5e3f7ba Add new feature
```
Save and close the file to continue

- _Modify the Author and Committer Information_

After saving and closing the file, Git will begin the interactive rebase. When it reaches the first commit that you want to modify, it will pause and allow you to make changes.

To modify the author and committer information, use the following commands:
```
git commit --amend --author="New Author Name <new-email@example.com>" --no-edit
git commit --amend --reset-author --no-edit
```
Replace New Author Name and new-email@example.com with the new name and email address that you want to use.

The first command modifies the author information of the commit, while the second command modifies the committer information. The --no-edit option tells Git not to open the commit message editor.

Once you have made your changes, use the following command to continue the rebase:

```
git rebase --continue
```
- _Finish the Rebase_


```
git rebase --continue
```

- _Push the Changes_


```
git push --force
```

This command tells Git to force push the modified commits to the remote repository, overwriting the existing commits. Be careful when using git push --force as it can cause data loss if used incorrectly.

Source - [Check Here ](https://saturncloud.io/blog/how-to-change-the-author-and-committer-name-and-email-of-multiple-commits-in-git/)



![Now Chill](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zye6wfpq8sbxe5khyqoy.gif)





Thankyou for reading. 💓
Please like, share, and follow for more dev content. 😉