diff --git a/cadetrdm/repositories.py b/cadetrdm/repositories.py
index f44edaeedcda3beee7ee0a16818642f25cf677b4..058b3653e9d0b9aae427bef78c3f26aff4104c55 100644
--- a/cadetrdm/repositories.py
+++ b/cadetrdm/repositories.py
@@ -624,11 +624,12 @@ class ProjectRepo(BaseRepo):
         print("Completed computations, commiting results")
         self.output_repo.add(".")
         try:
-            # This has to be from ._git.commit because this way it raises an error if no results have been written.
+            # This has to be using ._git.commit to raise an error if no results have been written.
             commit_return = self.output_repo._git.commit("-m", message)
             self.update_output_master_logs()
             print("\n" + commit_return + "\n")
         except git.exc.GitCommandError as e:
+            self.output_repo.delete_active_branch_if_branch_is_empty()
             raise e
         finally:
             self.remove_cached_files()
@@ -651,6 +652,7 @@ class ProjectRepo(BaseRepo):
         try:
             yield new_branch_name
         except Exception as e:
+            self.output_repo.delete_active_branch_if_branch_is_empty()
             raise e
         else:
             self.exit_context(message=results_commit_message)