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: Idba96a0866146f05dd1773d2149db3716c2cda2d
Reviewed-by: Scott Zawalski <>
Tested-by: David James <>
diff --git a/lib/ b/lib/
index 37a5673..7771e47 100644
--- a/lib/
+++ b/lib/
@@ -373,7 +373,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: