Ian Battin Ian Battin - 3 months ago 17
Git Question

Amending Git Author In a Published Repo

So I'd like to rewrite the history of a git repo. Now, all of the commits I'd like to change have already been published and pushed and I'm aware that rewriting history to published repos is bad practice. However, this repo has no forks and only a single master branch. Does this make rewriting history safer?

If so, what I'd want to do is change the author of all commits since a certain date by a specific author to the new author. Here is the code that github provides to do this:

#!/bin/sh

git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags


Can somebody change this to include my requirement of only changing commits since a certain date, say, after June 20th?

Also, is it possible to easily undo this change after seeing the effects?

Answer

Does this make rewriting history safer?

If somebody has cloned the commits that you are about to rewrite, they will be surprised when the history changes. If they have local commits on top of the original commits, some cleanup will be necessary, but should be doable at little effort. Since your repo doesn't seem to be much used, the risks seem negligible, and the worst case scenario is minor inconvenience.

Can somebody change this to include my requirement of only changing commits since a certain date, say, after June 20th?

I don't think you can specify a date limit to git filter-branch. But you can specify a range of revisions. I suggest to find the revision after which you want to make the change, for example by looking in git log --before 'June 20'. Once you have the SHA, change the end part of your script like this:

git filter-branch --env-filter '
# ... no change, as in your post
' -f SHA..

Also, is it possible to easily undo this change after seeing the effects?

Make a backup of the branch, for example with:

git branch master-bak

This creates a copy of the current branch. As you wrote you only have a master branch, I assume you are on master now, so this will create a backup of it.

If you're not happy with the result of the git filter-branch command above, you can restore the original state of the branch from the backup with:

git reset --hard master-bak
Comments