How to change the author information in the commit history of a 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.
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:
- Do you need to change the author information before you make a commit?
- Are the commits local or remote?
- 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"
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"
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"
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>"
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
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
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
Run through each of the commits and edit accordingly.
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 from GitHub and save it to an easily-accessible location.
- Change the permissions of the script file to allow it to execute:
chmod +x changeauthor.sh
- Navigate into the repository with the incorrect commit history
cd path/to/repo
Alternatively, you can run from anywhere by passing the --git-dir
and --work-tree
flags.
- Run the script (with or without flags)
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:
changeauthor.sh [OPTIONS]...
sh changeauthor.sh [OPTIONS]...
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
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 "$@"
}
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.
Warning: By passing the
--force
flag (along with all other required flags), there is no turning back. Once you start the script, the process will start and can severely damage your repository if used incorrectly.
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.