2

On my GitHub repository, there is an open pull request to merge changes from branch blTool-importFile-test into master. Several commits on new-files were accidentally committed with unreachable internal email addresses, and I would like to correct this before merging into master.

Commit history for <code>blTool-importFile-test</code>

Note that this issue is similar to "Change the author and committer name and e-mail of multiple commits in Git". The main difference is that I only want to change the author on a select few commits unique to the new-files branch, as opposed to rewriting history for my entire project.

Is it possible to change the author for commits on my blTool-importFile-test branch without affecting the history of master?

Community
  • 1
  • 1
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225

1 Answers1

3

Option 1

It seems you want to change the author information if the email address is ***@***.internal, you can still use the way:

$ git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_EMAIL" = "***@***.internal" ];
        then
                GIT_AUTHOR_NAME="new name";
                GIT_AUTHOR_EMAIL="new email";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

This will search in the whole commit history.

Option 2

If you don’t want to search in all commit histories, you can also edit a certain commit author information separately. Such as you want to change the author for commitA, you need to find the parent of commitA (commit before commitA), and then use below steps:

git rebase -i <parent of commitA> -p

input i, and change pick as edit for commitA: edit commitA. Then enter Esc and :wq in interactive window.

git commit --amend --author="name <email address>"
git rebase --continue

Then change the author for other commits use the same method.

Note: -p (--preserver-merges) will keep the structure as merge. And when the rebase handling the merge commit, there may be has conflict, you should modify/save the conflict files as you did in merge, and then use git add . and git rebase --continue.

Community
  • 1
  • 1
Marina Liu
  • 36,876
  • 5
  • 61
  • 74
  • Option A won't work since some older commits in master are attributed to `.internal`. Though it is very clever and I'm sure to make use of it in the future. – Stevoisiak Apr 25 '17 at 02:47
  • Option B only amend the commit you edit, it won't mess up other commits. – Marina Liu Apr 25 '17 at 02:55
  • Ah, ok. That will work nicely. I still need to run that command for each commit I want to change, but it'll work. The commits I show in that screenshot aren't the only commits I need to change. – Stevoisiak Apr 25 '17 at 03:26
  • You need to use the way separately for the commits you want to change. And it's more efficient than option A if git repo has long histories to search. – Marina Liu Apr 25 '17 at 05:21
  • Option B worked fairly well for me! There are 2 things to note though. 1) I changed the quote marks from ” to " in the amend author command. 2) Rebasing did not preserve my merge commits, meaning I had to solve a minor merge conflict while rebasing. Overall though, this worked great! – Stevoisiak Apr 26 '17 at 14:25
  • Turns out the merge commits not being preserved caused caused issues when comparing the git history of `master` and `blTool-importFile-test`. Unfortunately I've already force pushed the updated branch, so I can't try again with `--preserve-merges` – Stevoisiak Apr 26 '17 at 16:38
  • Yes, the quote should be `"`. And sorry for not mention how to preserver merges during base clearly. But one good thing is that you still have chance to make the commit histories go back as original: on `blTool-importFile-test` branch, use `git reset --hard ` (it seems `61204` is point to `blTool-importFile-test` branch according your screenshot, so you can use `git reset --hard 61204`). And then have a try by `git rebase -i -p`. I updated my answer, please check details in it. – Marina Liu Apr 27 '17 at 06:40