7

I have various private GitHub repos and use Sublime Merge to manage my commits.

I want to change all the previous commit author details:

From Name: This, Email: this@domain.com

To: Name: That, Email: that@domain.com

I have therefore followed these instructions from GitHub and amended the code to the following:

#!/bin/sh

git filter-branch -f --env-filter '

CORRECT_NAME="That"
CORRECT_EMAIL="that@domain.com"

export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"

' --tag-name-filter cat -- --branches --tags

On one of the repos this worked and before I used git push --force --tags origin 'refs/heads/*' from the instrucitons Sublime Merge showed the potential changes and after running the push all the commits were now updated to the desired details.

All good, so I thought, until I tried this with several of my other repositories and no change shows in sublime and the push does nothing. I have no idea why there is a difference. The other repositories are similar in the fact they all have the same original committer.

Why does this not work for the other repos and how can I fix to allow me to do the changes?

bigdaveygeorge
  • 947
  • 2
  • 12
  • 32
  • How exactly are you running this script (for example, what is the working directory, what command do you use to run it)? What was the output for the one repository where it worked? Was the output different for the other repositories? Were there any error messages? – mkrieger1 Jan 04 '20 at 12:14
  • 1
    Do the other repositories have the same *author and committer* as the one were the script worked, or *only the same author*, or *only the same committer* (you say "same commiter" in the question text, but the script changes the author)? – mkrieger1 Jan 04 '20 at 12:17
  • Thanks for your help, it appears I was missing both AUTHOR and COMMITTER data, see my answer below. – bigdaveygeorge Jan 04 '20 at 12:31

2 Answers2

19

A better option would be to use the new tool git filter-repo, which replaces BFG and git filter-branch.
See its user manual.

To modify username and emails of commits, you can create a mailmap file in the format accepted by git-shortlog.
For example, if you have a file named my-mailmap you can run

git filter-repo --mailmap my-mailmap

and if the current contents of that file are as follows (if the specified mailmap file is version controlled, historical versions of the file are ignored):

Correct Name <correct@email.com> <old@email.com>

then we can update username and/or emails based on the specified mapping.

See git shortlog "mapping author" section for the exact syntax of a mailmap file.

Or, with callbacks:

git-filter-repo --name-callback 'return name.replace(b"OldName", b"NewName")' \
   --email-callback 'return email.replace(b"old@email.com", b"new@email.com")'
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for this information, I've already changed the author details on my repos using the old method I posted but I will give this a try soon on a test repo, seems like an easier way for future. – bigdaveygeorge Jan 05 '20 at 10:30
3

I figured this out looks I wasn't including both AUTHOR and COMMITTER details:

In terminal within repo directory:

git filter-branch -f --env-filter "GIT_AUTHOR_NAME='New Name'; GIT_AUTHOR_EMAIL='new@email.com'; GIT_COMMITTER_NAME='New Name'; GIT_COMMITTER_EMAIL='new@email.com';" HEAD

Then:

git push --force --tags origin 'refs/heads/*'
bigdaveygeorge
  • 947
  • 2
  • 12
  • 32