DEV Community

loading...
Cover image for How to change the author information in the commit history of a git repository

How to change the author information in the commit history of a git repository

adamdehaven profile image Adam DeHaven Originally published at adamdehaven.com Updated on ・5 min read

When working on both work-related and personal projects, oftentimes you need to swap back and forth between git credentials in order to push to the corresponding repository... and sometimes, you forget. 🤷‍♂️

In this post, I'll provide a few different options to quickly change the author name/email for commits in your repository's history, swapping out the wrong commit author (or incorrect author details) with the correct information. First, we'll go through some of the safer ways to edit commits, and finally, I'll share a custom script that will do the heavy-lifting for you.

What are you trying to accomplish?

The way to change commit author information depends on the situation you find yourself in:

  1. Do you need to change the author information before you make a commit?
  2. Are the commits local or remote?
  3. How many commits do you need to alter? Are we talking about changing a LOT of commits (or maybe even all of them)?

Change author info before making a commit

If you need to change your identity before making a commit, there are three ways to update the info. None of the methods listed in this section will impact previous commits, only commits made from this point forward.

Change the author details for the next commit only

If you just need to change the author email/name for the next commit only, you can pass the --author flag with the desired info along with your commit message:

git commit --author="Marty McFly <marty.mcfly@thepinheads.com>" --message="Changing text on prom banner"
Enter fullscreen mode Exit fullscreen mode

Change the author info for the current repository

To change the author information that is used for all future commits in the current repository, you can update the git config settings so that they only apply here:

# Navigate to repository
cd path/to/repository

git config user.name "Marty McFly"
git config user.email "marty.mcfly@thepinheads.com"
Enter fullscreen mode Exit fullscreen mode

Change the author name and email globally

To update the author information your computer uses on a global level (e.g. for all repositories), add the --global flag:

git config --global user.name "Marty McFly"
git config --global user.email "marty.mcfly@thepinheads.com"
Enter fullscreen mode Exit fullscreen mode

Editing the history of previous commits (safely)

So you need to edit the author information of commit(s) that have already been made? First let's go over the safe(r) methods of editing your commit history.

Edit the last commit

If you just need to edit the last commit, the --amend flag is your friend:

git commit --amend --author="Marty McFly <marty.mcfly@thepinheads.com>"
Enter fullscreen mode Exit fullscreen mode

Editing multiple commits with interactive rebase

The easiest (and safest) way to edit the commit history is by using git's interactive rebase command.

First, find the hash of the commit right before the one you would like to edit and pass it to the rebase command:

# Passing the last "good" commit hash
git rebase -i 9b4351e2
Enter fullscreen mode Exit fullscreen mode

Then, mark each commit you would like to modify with the edit keyword:

edit 9b4351e2 Changing text on prom banner
edit 4d0f36c0 Setting flux capacitor to 1955
pick f4b39b80 Adding plutonium
Enter fullscreen mode Exit fullscreen mode

Git will step you through each commit, allowing you to edit (or not) as you desire:

Stopped at 9b4351e2... Changing text on prom banner
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Run through each of the commits and edit accordingly.

git commit --amend --author="Marty McFly <marty.mcfly@thepinheads.com>" --no-edit

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Edit all history using a git filter-branch script (danger-zone)

I have implemented the git filter-branch command into a custom script that can be used in interactive mode, or passively via option flags.

WARNING
This action is destructive to your repository's history. If you're collaborating on a repository with others, it's considered bad practice to rewrite published history.

You should only rewrite history if absolutely necessary.

Running this script rewrites history for all repository collaborators. After completing these steps, any person with forks or clones must fetch the rewritten history and rebase any local changes into the rewritten history.

Instructions

  • Download the script from GitHub and save it to an easily-accessible location.

    GitHub logo adamdehaven / change-git-author

    Update the commit history of your git repository to resolve incorrect author information.

  • Change the permissions of the script file to allow it to execute:

chmod +x changeauthor.sh
Enter fullscreen mode Exit fullscreen mode
  • Navigate into the repository with the incorrect commit history
cd path/to/repo
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can run from anywhere by passing the --git-dir and --work-tree flags.

  • Run the script (with or without flags)
../path/to/changeauthor.sh --old-email emmett.brown@example.com --new-email marty.mcfly@thepinheads.com --name "Marty McFly" --remote origin
Enter fullscreen mode Exit fullscreen mode

If you did not change the permissions to allow execution, you can also call the script with either of the following:

bash ./changeauthor.sh [OPTIONS]...

sh ./changeauthor.sh [OPTIONS]...
Enter fullscreen mode Exit fullscreen mode

If you run the script with no option flags, you will be prompted for the needed values via interactive prompts. The script will then proceed to update your local repository and push the changes to the specified remote.


If you would like to suppress the git-filter-branch warning, simply add the following line the ~/.bashrc file on your computer:

export FILTER_BRANCH_SQUELCH_WARNING=1
Enter fullscreen mode Exit fullscreen mode

If you prefer to set up the script as a function you can call from anywhere, add the following function to your ~/.bashrc file:

function changegitauthor() {
  # Update the path to point to the absolute path of the script on your computer
  bash /c/absolute/path/to/change-git-author/changeauthor.sh "$@"
}
Enter fullscreen mode Exit fullscreen mode

You may pass options (as flags) directly to the script, or pass nothing to run the script in interactive mode. All of the available options are outlined below:

old-email

  • Usage: -o, --old-email
  • Example: emmett.brown@example.com

The old/incorrect email address of the author you would like to replace in the commit history.

new-email

  • Usage: -e, --new-email
  • Example: marty.mcfly@thepinheads.com

The new/corrected email address to replace in commits matching the old-email address.

new-name

  • Usage: -n, --new-name
  • Example: Marty McFly

The new/corrected name for the new commit author info. (Be sure to enclose name in quotes)

remote

  • Usage: -r, --remote
  • Default: origin
  • Example: github

The name of the repository remote you would like to alter.

force

  • Usage: -f, --force

Allows the script to run successfully in a non-interactive shell (assuming all required flags are set), bypassing the confirmation prompt.

If you do not pass a value to the --remote flag when using --force, the default remote (origin) will be used.

git-dir

  • Usage: -d, --git-dir

Set the path to the repository (".git" directory) if it differs from the current directory. It can be an absolute path or relative path to current working directory.

This option should be used in conjunction with the --work-tree flag.

work-tree

  • Usage: -w, --work-tree

Set the path to the working tree. It can be an absolute path or a path relative to the current working directory.

help

  • Usage: -h, -?, --help

Show the help content.

version

  • Usage: -v, -V, --version

Show version information.

Discussion (0)

pic
Editor guide