Automatically running a series of commands on different git branches.

How do you automatically run a series of commands on different git branches?

I wanted to compile my project and then run a benchmarking tool on several branches.

I was repeating this:

git checkout master
make
bench './bin/my-app --file=large-input.csv'

git checkout refactor-branch
make
bench './bin/my-app --file=large-input.csv'

...

I wanted something like:

compare-branches \
"make && bench './bin/my-app --file=large-input.csv'" \
master \
refactor-branch

After googling and reaching out on Twitter I wrote a solution:

#!/bin/bash

# Usage:
# ./compare-branches.sh "./some-command arg-1 arg-2" branch-1 branch-2
#
# https://twitter.com/elliotthilaire/status/1083859808086720512
# Thanks to @schneems, @tosbourn, and @matthewrudy for their input.

current_branch=$(git symbolic-ref --short HEAD)

command=$1
commits=${@:2}

echo "running $command on $commits"
echo "----------------------------------------"

for commit in $commits
do
  $(git checkout $commit)
  eval $command
  echo "----------------------------------------"
done

$(git checkout $current_branch)

Here's a gist and thank you to everyone who contributed.

If you like this post or see something I can improve, tell me.