Git rebase to change/add author of specific commits
I recently have to change authors and add authors to some commit (do not ask why authors were missing… but it’s good to have right author not to steal any code :))
I use to use git rebase -x git commit --amend --no-edit --author="New name <new@mail>" but how about change on some old commit only. I do not find it straight away on google … but I finished with this solution
First get the list of commit-sha I want to changes, in my case some are author change, some are add co-author.
git log --pretty=format:"%H" 74c96ba540..0ab90b4e1e > change_to_author1
git log --pretty=format:"%H" 6a9c267532..d04ec82769 > add_author2_as_co_authorNow the idea is to git rebase starting-sha -x ./update-script.sh wish a script that apply the change to commits only if the last picked commit-sha is in one of the list. But getting the last commit-sha is not as easy as expected, since my usual git rev-parse HEAD will give new commit-sha after the first modification.
After digging into the git rebase process and available file, testing with git rebase -i I ended up with a “solution” but that was not directly adaptable to non interactive rebase : using the content of .git/rebase-merge/done (last line is pick the commit, but with -x last line is exec …) then I triend to use .git/rebase-merge/rewritten-list which seems to work, unless for the first commit, one solution might be to rebase from strating-sha~1, but finally I use done with a done content, filter last pick command
#!/bin/bash
# adapted/simplified from real code, might contains type ! always read and understand code before running on your side
author1="Author One <moc.e1770838731no@ro1770838731htua1770838731>"
author2="Author Two <rf.ow1770838731t@owt1770838731.roht1770838731ua1770838731>"
tag=`cat .git/rebase-merge/done | grep pick | tail -n1 |cut -f2 -d\ `
is_author1=$(cat change_to_author1 | grep -c $tag)
if [ $is_author1 -ne 0 ]; then
git commit --no-edit --amend --author="$author1"
fi
is_coauthor=$(cat add_author2_as_co_author | grep -c $tag)
if [ $is_coauthor -ne 0 ]; then
OLD_MSG=$(git log --format=%B -n1)
git commit --amend -m"$OLD_MSG" -m"Co-authored-by: $author2"
fi
Leave a Reply