-# Compile each commit. Create branch to avoid "detached HEAD" state, and also to
-# keep a record of what was sent:
-git rebase --exec=${compile_script} ${COMMIT_START_SHA1}
+# Create temporary branch for compilation, based on compile_branch:
+temp_compile_branch="${patches_branch}_compile"
+
+if git branch | grep -q ${temp_compile_branch}; then
+ git branch -D ${temp_compile_branch}
+fi
+
+git checkout -b ${temp_compile_branch} ${compile_branch}
+
+# Make sure temp_compile_branch is rebased on base_branch:
+git rebase ${base_branch}
+
+# Apply all of our commits to temp_compile_branch:
+git cherry-pick ${COMMIT_START_SHA1}..${COMMIT_END_SHA1}
+
+# Compile and test each commit:
+git rebase --exec=${compile_script} ${compile_branch}
+