diff --git a/hgext/rebase.py b/hgext/rebase.py
index bb14a81ce6473c6c59e681a71c414d46438c029e_aGdleHQvcmViYXNlLnB5..6f92d6bd297265de64c2592893c5a5d945f59f38_aGdleHQvcmViYXNlLnB5 100644
--- a/hgext/rebase.py
+++ b/hgext/rebase.py
@@ -397,13 +397,14 @@
             if dest.closesbranch() and not keepbranchesf:
                 ui.status(_('reopening closed branch head %s\n') % dest)
 
-        if keepbranchesf and collapsef:
-            branches = set()
-            for rev in state:
-                branches.add(repo[rev].branch())
-                if len(branches) > 1:
-                    raise error.Abort(_('cannot collapse multiple named '
-                                        'branches'))
+        if keepbranchesf:
+            if collapsef:
+                branches = set()
+                for rev in state:
+                    branches.add(repo[rev].branch())
+                    if len(branches) > 1:
+                        raise error.Abort(_('cannot collapse multiple named '
+                            'branches'))
 
         # Rebase
         if not targetancestors: