How to change the author information in the commit history of a git repository
Tags: git, tutorial, script, author
Published: 2020-09-28 16:17:13

---
published: true
title: How to change the author information in the commit history of a git repository
description: 
tags: git, tutorial, script, author
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/1hp8t29utmnd71vc790e.png
canonical_url: https://www.adamdehaven.com/blog/update-commit-history-author-information-for-git-repository/
---

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. :man_shrugging:

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](#edit-all-history-using-a-git-filter-branch-script-danger-zone) 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:

``` sh
git commit --author="Marty McFly <marty.mcfly@thepinheads.com>" --message="Changing text on prom banner"
```

### 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:

``` sh
# Navigate to repository
cd path/to/repository

git config user.name "Marty McFly"
git config user.email "marty.mcfly@thepinheads.com"
```

### 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:

``` sh
git config --global user.name "Marty McFly"
git config --global user.email "marty.mcfly@thepinheads.com"
```

## 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:

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

### 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:

``` sh
# Passing the last "good" commit hash
git rebase -i 9b4351e2
```

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

``` sh
edit 9b4351e2 Changing text on prom banner
edit 4d0f36c0 Setting flux capacitor to 1955
pick f4b39b80 Adding plutonium
```

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

``` sh
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
```

Run through each of the commits and edit accordingly.

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

git rebase --continue
```

## 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](https://github.com/adamdehaven/change-git-author) from GitHub and save it to an easily-accessible location. {% github adamdehaven/change-git-author no-readme %}

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

``` sh
chmod +x changeauthor.sh
```

- Navigate into the repository with the incorrect commit history

``` sh
cd path/to/repo
```

Alternatively, you can run from anywhere by passing the [`--git-dir`](#git-dir) and [`--work-tree`](#work-tree) flags.

- Run the script (with or without flags)

``` sh
../path/to/changeauthor.sh --old-email emmett.brown@example.com --new-email marty.mcfly@thepinheads.com --name "Marty McFly" --remote origin
```

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

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

sh ./changeauthor.sh [OPTIONS]...
```

If you run the script with no [option flags](#old-email), 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:

``` sh
export FILTER_BRANCH_SQUELCH_WARNING=1
```

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

``` sh
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 "$@"
}
```

----

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](#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.