DEV Community

Cover image for Silly Git Mistakes! Fixing Author name in git history
Navdeep Mishra
Navdeep Mishra

Posted on • Updated on

Silly Git Mistakes! Fixing Author name in git history

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

How do we fix this

Relax............

Relax

I have solution

_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.

#!/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

Enter fullscreen mode Exit fullscreen mode

Here change your name and email to correct email.

After that make the script executable.

chmod +x update_author_info.sh
Enter fullscreen mode Exit fullscreen mode

Now just run the script

./update_author_info.sh

Enter fullscreen mode Exit fullscreen mode

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

git push origin --all --force
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Start an Interactive Rebase
git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Finish the Rebase
git rebase --continue
Enter fullscreen mode Exit fullscreen mode
  • Push the Changes
git push --force
Enter fullscreen mode Exit fullscreen mode

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

Now Chill

Thankyou for reading. πŸ’“
Please like, share, and follow for more dev content. πŸ˜‰

Top comments (0)