diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
index 3ef279074c77c3cf3f6b35f0f73dee2fdba5aa41_bWVyY3VyaWFsL2xvY2FscmVwby5weQ==..dd5a501cb97fbe6aea0fd25f8fc7451f5285ea21_bWVyY3VyaWFsL2xvY2FscmVwby5weQ== 100644
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -525,5 +525,6 @@
         except IOError:
             ds = ""
         self.opener("journal.dirstate", "w").write(ds)
+        self.opener("journal.branch", "w").write(self.dirstate.branch())
 
         renames = [(self.sjoin("journal"), self.sjoin("undo")),
@@ -528,6 +529,7 @@
 
         renames = [(self.sjoin("journal"), self.sjoin("undo")),
-                   (self.join("journal.dirstate"), self.join("undo.dirstate"))]
+                   (self.join("journal.dirstate"), self.join("undo.dirstate")),
+                   (self.join("journal.branch"), self.join("undo.branch"))]
         tr = transaction.transaction(self.ui.warn, self.sopener,
                                        self.sjoin("journal"),
                                        aftertrans(renames))
@@ -557,6 +559,8 @@
                 self.ui.status(_("rolling back last transaction\n"))
                 transaction.rollback(self.sopener, self.sjoin("undo"))
                 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
+                branch = self.opener("undo.branch").read()
+                self.dirstate.setbranch(branch)
                 self.invalidate()
                 self.dirstate.invalidate()
             else:
diff --git a/tests/test-hup.out b/tests/test-hup.out
index 3ef279074c77c3cf3f6b35f0f73dee2fdba5aa41_dGVzdHMvdGVzdC1odXAub3V0..dd5a501cb97fbe6aea0fd25f8fc7451f5285ea21_dGVzdHMvdGVzdC1odXAub3V0 100644
--- a/tests/test-hup.out
+++ b/tests/test-hup.out
@@ -4,4 +4,4 @@
 transaction abort!
 rollback completed
 killed!
-.hg/00changelog.i .hg/journal.dirstate .hg/requires .hg/store .hg/store/00changelog.i .hg/store/00changelog.i.a
+.hg/00changelog.i .hg/journal.branch .hg/journal.dirstate .hg/requires .hg/store .hg/store/00changelog.i .hg/store/00changelog.i.a
diff --git a/tests/test-rollback b/tests/test-rollback
index 3ef279074c77c3cf3f6b35f0f73dee2fdba5aa41_dGVzdHMvdGVzdC1yb2xsYmFjaw==..dd5a501cb97fbe6aea0fd25f8fc7451f5285ea21_dGVzdHMvdGVzdC1yb2xsYmFjaw== 100755
--- a/tests/test-rollback
+++ b/tests/test-rollback
@@ -13,3 +13,9 @@
 hg verify
 hg parents
 hg status
+
+# Test issue 902
+hg commit -m "test"
+hg branch test
+hg rollback
+hg branch
diff --git a/tests/test-rollback.out b/tests/test-rollback.out
index 3ef279074c77c3cf3f6b35f0f73dee2fdba5aa41_dGVzdHMvdGVzdC1yb2xsYmFjay5vdXQ=..dd5a501cb97fbe6aea0fd25f8fc7451f5285ea21_dGVzdHMvdGVzdC1yb2xsYmFjay5vdXQ= 100644
--- a/tests/test-rollback.out
+++ b/tests/test-rollback.out
@@ -16,3 +16,6 @@
 checking files
 0 files, 0 changesets, 0 total revisions
 A a
+marked working directory as branch test
+rolling back last transaction
+default