Change the author of commit commits starting from the template (in the message)

I have a repository where there are some (actually a lot) commits created by a program that looks like this:

Adding new domains: example.com 

It happened (due to incorrect configuration) that these commits were made on behalf of a colleague. Now I want to change these commits:

 Some Bot < bot@example.com > 

Github has a public script posted here to modify commits filtered by the old author. In this case, I want to change the author of the commits whose messages begin with the template: Adding new domains :. How can i do this?

+5
source share
2 answers

In this case, I think you will have to use the --commit-filter . This is the only way to simultaneously receive both a commit message (from stdin) and environment variables. The new script will have to fully reproduce all the functionality of commit-tree . Fortunately, it will not look the same as the GitHub script you linked.

First read the entire commit message in the Bash variable:

 commitmsg=$(cat) 

Now get the first line and check if it matches:

 firstline=$(echo "$commitmsg" | head -n1 | grep "Adding new domains:") if [ "$firstline" ]; then 

We matched the commit message, so we do a regular replacement, as in the GitHub script.

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

Now we have to call git commit-tree . A commit message is provided on stdin as we received it. Since our script acts as a surrogate for commit-tree , we also need to pass all the arguments. The environment variables that we (possibly modified) take care of themselves.

 echo "$commitmsg" | git commit-tree " $@ " 

This produces a hash on stdout, as we should do. This is where we are done!

Here is a copy of the completed script. YMMV, of course, but I tested it and it works great.

 #!/bin/bash git filter-branch --commit-filter ' CORRECT_NAME="Your Correct Name" CORRECT_EMAIL=" your-correct-email@example.com " commitmsg=$(cat) firstline=$(echo "$commitmsg" | head -n1 | grep "Adding new domains:") if [ "$firstline" ]; then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi echo "$commitmsg" | git commit-tree " $@ " ' --tag-name-filter cat -- --branches --tags 
+4
source

You can use the --env-filter and rev-list arguments ( --grep=<pattern> in this case) to limit the commits to the ones you want to change:

 #!/bin/sh git filter-branch --env-filter ' CORRECT_NAME="Your Correct Name" CORRECT_EMAIL=" your-correct-email@example.com " export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" ' --tag-name-filter cat -- --branches --tags --grep='Adding new domains' 

You can save this script with the git prefix - and put it somewhere in your PATH, for example. /usr/local/bin/git-change-author (make sure it is executable) and run it from your repository as:

 git change-author 

Don't forget git push --force since you rewrote the story.

+5
source

All Articles