Shell - batch revert and push to gerrit

less than 1 minute read

This post illustrates how to revert a commit in many branches, say 100 for example, and then push to gerrit server with reviewers added.

hash=<hash value of commit>
while read -r line ; do
    branch=${line#*/}
    br_tmp="${branch}_tmp"
    echo "Processing branch: $branch"

    # create local branch
    git checkout -b $br_tmp orgin/$branch

    # revert without prompt
    git revert $hash --no-edit

    # do ammend to add Change ID
    git commit --amend --no-edit

    # push to gerrit
    git push origin $br_tmp:refs/for/$branch%r=foo@mail.com,r=bar@mail.com
done < <(git branch -r --contains $hash)

There is another way to add reviewers or just send notification mail, take a look at Using Gerrit on Ubuntu.

Resources