Cleanup failed rebases in

If a rebase fails we should be nice and git rebase --abort so that we won't
leave the tree in a busted state.

TEST=ran prebuilt test suite

Reviewed-by: Chris Sosa <>
Reviewed-by: Scott Zawalski <>
Tested-by: David James <>
(cherry picked from commit cb2d970da51c6c9ae9123a4a47bad6252c608ca2)

Change-Id: Idd21c33a5b77bbc574958db4a1b94f6ed538e566
Reviewed-by: Scott Zawalski <>
Tested-by: David James <>
diff --git a/lib/ b/lib/
index 9b5b39d..bd2743a 100644
--- a/lib/
+++ b/lib/
@@ -339,7 +339,13 @@
   for retry in range(1, retries + 1):
       RunCommand(['git', 'remote', 'update'], cwd=cwd)
-      RunCommand(['git', 'rebase', '%s/%s' % (remote, push_branch)], cwd=cwd)
+      try:
+        RunCommand(['git', 'rebase', '%s/%s' % (remote, push_branch)], cwd=cwd)
+      except RunCommandError:
+        # Looks like our change conflicts with upstream. Cleanup our failed
+        # rebase.
+        RunCommand(['git', 'rebase', '--abort'], error_ok=True, cwd=cwd)
+        raise
       push_command = ['git', 'push', remote, '%s:%s' % (branch, push_branch)]
       if dryrun: