Force Rewrite Commit Authorship History Using Git Command Line

Print Friendly, PDF & Email

Force Rewrite Commit Authorship History Using Git Command Line

Have you ever been working in a git repo and pushed your changes, only to realize later on that you were committing as the wrong author, possibly because you sometimes use multiple user names in your GitHub app? In this post, we’re going to look at how to forcibly rewrite the commit authorship history in a git repo using git from the command line.

Finding The Commit In History After Which You’d Like To Change Authorship

The first thing we’re going to do is go to our repository on GitHub.com so we can browse the commit history.

git commit history

As we can see in the commit history, the top four commits (inside the red box) have the incorrect commit author listed. Let’s copy the SHA of the commit JUST BEFORE (one below) the first commit with the incorrect author by clicking on the copy button next to the SHA.

Using PowerShell To Interact With The Git Repo

In this example, we’re going to use PowerShell, but any other terminal will work just as well. In Windows Explorer, navigate to the local git repo on your computer. Once in the repo root directory, hold Shift and then right click on any white area in the Window. Choose PowerShell from the context menu. This will open up PowerShell in the repo root directory. The first thing we want to do here is change the global user.name and user.email to the correct author we want to use to rewrite the commit authorship history. This is achieved using the following commands:

$ git config --global user.name "Daniel Koman"
$ git config --global user.email "dankoman30@gmail.com"

Next, we’ll use another command to reset the author of all commits after a specific commit in history. Using the commit SHA you copied to your clipboard previously, enter the following command (replacing my commit SHA with the one you copied):

$ git rebase -i d62e3b221b69d110a0df54849e7b9132ac80aed7 -x "git commit --amend --reset-author -CHEAD"

The Rebase Review Screen

After pressing Enter to execute the previous command, we’re presented the following screen:

review rebase screen

It might look a little intimidating, but this is simply a screen to review the changes about to be made via rebase. As we can see from the screenshot, the final 4 commits in history are being rebased. This is exactly what we want, so let’s proceed.

Next, we’re going to press Ctrl + C on the keyboard. This will give us the ability to input command text into the bottom line of the review screen, as seen below:

Let’s type :wq here (which is an abbreviation for Write and Quit, in case you were wondering), then press Enter to confirm all of the changes in the review screen. After successfully rebasing the commit history, we’re brought back to the main terminal, where we can see the changes that have been made in the local repo:

successful rebase of local repo

Now our local repository is exactly as we want it, with the global user.name and user.email set as the commit author for the commits in question. We’re not done yet, though, as we still need to push our changes to the remote repository on GitHub.

Force Pushing The Local Repo To Overwrite The Commit History On The Remote Repo

To force push our changes and rewrite the remote repo, let’s enter the following command in the terminal:

$ git push --force

The –force argument is key here, as this command will forcibly overwrite the commit history in the remote repo with the commit history from our local repo. Force pushing is dangerous, as it will literally overwrite our commit history in the remote repo on GitHub, so be sure you actually want to do this before proceeding! Backing up your remote repo first may be pertinent if you’re worried about this.

After entering this command, we can see our changes being successfully written to the remote:

force pushing local changes to remote

That’s it! We’ve successfully changed the commit author for all of the commits in question, as evidenced by navigating back to our repo on GitHub and examining the commit history:

successful rewrite of commit history on remote

The rewritten commits all have new SHAs, and our commit history now was the correct authorship!

Leave a Reply

Your email address will not be published. Required fields are marked *